Pentiums Posted March 4, 2009 Report Share Posted March 4, 2009 Pēdējo komentēto rakstu panelis, raksti neatkārtojas. Pie pēdējā komentētā raksta parādas niks, datums un raksta nosaukums. Pēdējie komentētie raksti parādas korekti, bet pie raksta klāt parādas nevis tas niks un tas datums kurš pēdējo reizi to rakstu komentēja bet pirmo reizi! Bet vajadzētu pēdējo komentāra niku un datumu. SELECT DISTINCT (komentari.tid), komentari.datums as datumz, raksti.id as raksta_id, raksti.virsraksts as raksta_nosaukums, lietotaji.lietotajs niks FROM komentari LEFT JOIN raksti ON raksti.id=komentari.tid LEFT JOIN lietotaji ON lietotaji.id=komentari.uid WHERE komentari.tips='raksts' GROUP BY komentari.tid ORDER BY komentari.id DESC LIMIT 5 komentari (tid ir raksta id un uid ir lietotāja id) id|datums|tid|uid|komentars Quote Link to comment Share on other sites More sharing options...
mounkuls Posted March 4, 2009 Report Share Posted March 4, 2009 nemaniju tabulā komentari lauku `tips`... Quote Link to comment Share on other sites More sharing options...
Pentiums Posted March 5, 2009 Author Report Share Posted March 5, 2009 jā ir arī tips bet tas neko šeit nemaina Quote Link to comment Share on other sites More sharing options...
Aleksejs Posted March 5, 2009 Report Share Posted March 5, 2009 Mēģini pielikt MAX(komentari.datums) SELECT DISTINCT (komentari.tid), MAX(komentari.datums) as datumz, raksti.id as raksta_id, raksti.virsraksts as raksta_nosaukums, lietotaji.lietotajs niks FROM komentari LEFT JOIN raksti ON raksti.id=komentari.tid LEFT JOIN lietotaji ON lietotaji.id=komentari.uid WHERE komentari.tips='raksts' GROUP BY komentari.tid ORDER BY komentari.id DESC LIMIT 5 Quote Link to comment Share on other sites More sharing options...
Pentiums Posted March 5, 2009 Author Report Share Posted March 5, 2009 tas jau tika mēģināts, diemžēl izvilka pareizi TIKAI datumu bet lietotāja niks palika tas pats Quote Link to comment Share on other sites More sharing options...
Aleksejs Posted March 5, 2009 Report Share Posted March 5, 2009 SELECT DISTINCT (komentari.tid), komentari.datums as datumz, raksti.id as raksta_id, raksti.virsraksts as raksta_nosaukums, lietotaji.lietotajs niks FROM komentari LEFT JOIN raksti ON raksti.id=komentari.tid LEFT JOIN lietotaji ON lietotaji.id=komentari.uid WHERE komentari.tips='raksts' AND komentari.id IN ( SELECT k2.id FROM komentari AS k2 WHERE k2.komentari.tid=raksti.id AND k2.datums IN ( SELECT MAX(k3.datums) FROM komentari AS k3 WHERE k3.tid=raksti.id ) ) GROUP BY komentari.tid ORDER BY komentari.id DESC LIMIT 5 Kaut kā šadi, tad varbūt. Quote Link to comment Share on other sites More sharing options...
Pentiums Posted March 5, 2009 Author Report Share Posted March 5, 2009 Liels paldies Aleksej! :) Quote Link to comment Share on other sites More sharing options...
Aleksejs Posted March 5, 2009 Report Share Posted March 5, 2009 Lūdzu, :) Man gan šķiet, ka tur vēl var optimizēt un optimizēt... un, iespējams, ka pavisam savādāk to uzrakstīt. Pagaidi vēl, ko Gints pateiks :) Intereses pēc iemet, kāds izskatās execution plāns. EXPLAIN viss tas garais vaicājums Quote Link to comment Share on other sites More sharing options...
Pentiums Posted March 5, 2009 Author Report Share Posted March 5, 2009 $result = mysql_query("SELECT DISTINCT (komentari.tid), komentari.datums as datumz, raksti.curl as raksta_curl, raksti.id as raksta_id, raksti.virsraksts as raksta_nosaukums, lietotaji.lietotajs as niks, lietotaji.id as lietotaja_id FROM komentari LEFT JOIN raksti ON raksti.id=komentari.tid LEFT JOIN lietotaji ON lietotaji.id=komentari.uid WHERE komentari.tips='raksts' AND komentari.id IN ( SELECT k2.id FROM komentari AS k2 WHERE k2.tid=raksti.id AND k2.datums IN ( SELECT MAX(k3.datums) FROM komentari AS k3 WHERE k3.tid=raksti.id ) ) GROUP BY komentari.tid ORDER BY komentari.id DESC LIMIT 5"); $hr = ''; while($data = mysql_fetch_array($result)) { echo $hr.'<span id="small"><a href="?view&profile='.$data['lietotaja_id'].'">'.$data['niks'].'</a> '.dienas(date('Y-m-d H:i', $data['datumz'])).'</span><br><b><a href="?raksts='.$data['raksta_id'].'&'.$data['raksta_curl'].'">'.$data['raksta_nosaukums'].'</a></b>'; $hr = '<hr>'; } Quote Link to comment Share on other sites More sharing options...
Aleksejs Posted March 5, 2009 Report Share Posted March 5, 2009 Nē, biju domājis: EXPLAIN SELECT DISTINCT (komentari.tid), komentari.datums as datumz, raksti.id as raksta_id, raksti.virsraksts as raksta_nosaukums, lietotaji.lietotajs niks FROM komentari LEFT JOIN raksti ON raksti.id=komentari.tid LEFT JOIN lietotaji ON lietotaji.id=komentari.uid WHERE komentari.tips='raksts' AND komentari.id IN ( SELECT k2.id FROM komentari AS k2 WHERE k2.komentari.tid=raksti.id AND k2.datums IN ( SELECT MAX(k3.datums) FROM komentari AS k3 WHERE k3.tid=raksti.id ) ) GROUP BY komentari.tid ORDER BY komentari.id DESC LIMIT 5[/sql] Quote Link to comment Share on other sites More sharing options...
Pentiums Posted March 5, 2009 Author Report Share Posted March 5, 2009 Kas tur ko explain`ot. Jāizvelk pēdējie unikālie komentāri pēc `tid` (raksta id) pievienojot lietotāju tabulu pēc `uid` (lietotāja id) un rakstu tabulu pēc `tid`. Jāizvada pieci pēdējie komentētie raksti, tie nedrīkst atkārtoties, tapēc izmantojam DISTINCT, katram rakstam klāt piemetinam raksta jaunākā komentāra autoru un komentāra datumu. Quote Link to comment Share on other sites More sharing options...
Aleksejs Posted March 5, 2009 Report Share Posted March 5, 2009 Pentium - vai Tu zini, ko dara EXPLAIN? ;) Ja nezini, tad ātri izlasi :) http://dev.mysql.com/doc/refman/5.0/en/explain.html http://hackmysql.com/case1 Quote Link to comment Share on other sites More sharing options...
Pentiums Posted March 5, 2009 Author Report Share Posted March 5, 2009 Vaii.. +-----+------------+-----------+--------------------------------------------------+----------+--------------+ | tid | datumz | raksta_id | raksta_nosaukums | niks | lietotaja_id | +-----+------------+-----------+--------------------------------------------------+----------+--------------+ | 101 | 1236240840 | 101 | raksts2 | lietotajs4| 1 | | 100 | 1236102567 | 100 | raksts5 | lietotajs1| 2 | | 99 | 1236091050 | 99 | raksts8 | lietotajs4| 1 | | 87 | 1235989530 | 87 | raksts3 | lietotajs9| 3 | | 94 | 1235940158 | 94 | raksts1 | lietotajs4| 1 | +-----+------------+-----------+--------------------------------------------------+----------+--------------+ 5 rows in set (0.03 sec) Quote Link to comment Share on other sites More sharing options...
Aleksejs Posted March 5, 2009 Report Share Posted March 5, 2009 Nu, Pentiūūūūm! :D Nevis paša SQLa rezultāts mani interesē, bet gan - vaicājuma izpildes plāns - tas izskatīsies kaut kā šitā: mysql> EXPLAIN -> SELECT attach_id as total_attachment FROM phpbb_attachments -> WHERE post_id IN (163246, 164224, 164894, 165146, 167931); +-------------------+-------+---------------+-------------------+---------+------+-------+--------------------------+ | table | type | possible_keys | key | key_len | ref | rows | Extra | +-------------------+-------+---------------+-------------------+---------+------+-------+--------------------------+ | phpbb_attachments | index | NULL | attach_id_post_id | 6 | NULL | 61834 | Using where; Using index | +-------------------+-------+---------------+-------------------+---------+------+-------+--------------------------+ Tikai, protams, savu Selectu aiz EXPLAIN ieraksti :) Quote Link to comment Share on other sites More sharing options...
Pentiums Posted March 5, 2009 Author Report Share Posted March 5, 2009 mysql> EXPLAIN SELECT DISTINCT (komentari.tid), komentari.datums as datumz, raks ti.curl as raksta_curl, raksti.id as raksta_id, raksti.virsraksts as raksta_nosa ukums, lietotaji.lietotajs as niks, lietotaji.id as lietotaja_id -> FROM komentari LEFT JOIN raksti ON raksti.id=komentari.tid -> LEFT JOIN lietotaji ON lietotaji.id=komentari.uid -> WHERE komentari.tips='raksts' AND komentari.id IN ( -> SELECT k2.id FROM komentari AS k2 WHERE k2.tid=raksti.id AND k2.datums IN ( -> SELECT MAX(k3.datums) FROM komentari AS k3 WHERE k3.tid=raksti.id -> ) -> ) -> GROUP BY komentari.tid -> ORDER BY komentari.id DESC LIMIT 5 ->; +----+--------------------+-----------+-----------------+---------------+---------+---------+--------------------------+------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+-----------+-----------------+---------------+---------+---------+--------------------------+------+----------------------------------------------+ | 1 | PRIMARY | komentari | ALL | NULL | NULL | NULL | NULL | 156 | Using where; Using temporary; Using filesort | | 1 | PRIMARY | raksti | eq_ref | PRIMARY | PRIMARY | 4 | niknais_v2.komentari.tid | 1 | Using where | | 1 | PRIMARY | lietotaji | eq_ref | PRIMARY | PRIMARY | 4 | niknais_v2.komentari.uid | 1 | | | 2 | DEPENDENT SUBQUERY | k2 | unique_subquery | PRIMARY | PRIMARY | 4 | func | 1 | Using where | | 3 | DEPENDENT SUBQUERY | k3 | ALL | NULL | NULL | NULL | NULL | 156 | Using where | +----+--------------------+-----------+-----------------+---------------+---------+---------+--------------------------+------+----------------------------------------------+ 5 rows in set (0.02 sec) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.