Vilsol Posted March 27, 2011 Report Share Posted March 27, 2011 Tātad pieņemsim, ka man ir db kurā vienā tabulā ir komentāri un otrā useri. Lai izveidotu topu dilstošā secībā man vajadzēs ar while iet cauri visam useri db un skaitīt cik katram ir komentāri? Pēctam kā? Quote Link to comment Share on other sites More sharing options...
briedis Posted March 27, 2011 Report Share Posted March 27, 2011 Protams, ka nē. Ir tak sql atbilstošas funkcijas, ko var labi izmantot apvienojumā ar JOIN. http://www.mysqltutorial.org/mysql-aggregate-functions.aspx Quote Link to comment Share on other sites More sharing options...
Vilsol Posted March 27, 2011 Author Report Share Posted March 27, 2011 Tātad GROUP BY tiek uzskatīts kā galvenais un pārējie tiek viņam pielīdzināti un pēctam liekot ORDER BY izvēlies pēc kuriem skaitļiem kārtot? Ja tā tad paldies! Quote Link to comment Share on other sites More sharing options...
briedis Posted March 27, 2011 Report Share Posted March 27, 2011 (edited) SELECT u.id, u.username, COUNT(c.id) as comment_count FROM users u LEFT JOIN comments c ON c.user_id = u.id GROUP BY u.id ORDER BY comment_count DESC Tā tas varētu izskatīties. Edited March 27, 2011 by briedis 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.