rebuilder Posted February 14, 2011 Report Share Posted February 14, 2011 (edited) Lieta sekojoša ar get nolasu id teiksim 55 tad veicu pieprasījumu sql lai atlasa datus pēc ši id. Tik tālu viss ok. Bet ta sākas problēma, ka vajag noteikt nākošo un iepriekšējo tuvāko id, nav jau problēma tur uzrakstīt ar if bla bla bla, ja vien es zinātu ka tiešām id 54 vai 56 tiešām tur būs, reāli pēc 55 var sekot kaut vai 105. Ceru domu uztvērāt. pagaidām izmantoju ... WHERE id > $id LIMIT 1 ... un ... WHERE id < $id LIMIT 1 ... bet nepatīk ka jātaisa 2 pieprasījumi, varbūt ir kāds labāks risinājums? Edited February 14, 2011 by rebuilder Quote Link to comment Share on other sites More sharing options...
marcis Posted February 14, 2011 Report Share Posted February 14, 2011 Nav nekā īpaši labāka. Vari lietot UNION, kas pieprasījumu skaitu samazinās līdz vienam, bet ērtāks šis risinājums gan nebūs. Quote Link to comment Share on other sites More sharing options...
v3rb0 Posted February 14, 2011 Report Share Posted February 14, 2011 vēl viens variants. SELECT previous.ID AS previousID, current.ID AS ID, next.ID AS nextID FROM table1 current LEFT JOIN table1 previous ON current.ID > previous.ID LEFT JOIN table1 next ON current.ID < next.ID WHERE current.ID = 55 ORDER BY previous.ID DESC, next.ID ASC LIMIT 1 Quote Link to comment Share on other sites More sharing options...
rebuilder Posted February 14, 2011 Author Report Share Posted February 14, 2011 Paldies, par ieteikumiem :) Quote Link to comment Share on other sites More sharing options...
Gints Plivna Posted February 14, 2011 Report Share Posted February 14, 2011 Grupēšanas f-jas min, max ir īstā atbilde. mysql> desc ids; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.03 sec) mysql> select max(id) prev from ids where id<5 -> union all -> select min(id) nxt from ids where id>5; +------+ | prev | +------+ | 3 | | 77 | +------+ 2 rows in set (0.00 sec) 1kārt mazāk jāraksta, 2kārt izpildes plāns izskatās labāks (ātrāks) nekā order by .. limit 1 vai iepriekšējā kolēģa ieteiktais. Gints Plivna http://datubazes.wordpress.com Quote Link to comment Share on other sites More sharing options...
rebuilder Posted February 14, 2011 Author Report Share Posted February 14, 2011 Šis jau vairāk uz to ko tieši meklēju :) Paldies! 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.