ted_chou12 Posted February 13, 2007 Share Posted February 13, 2007 This time, I wish to order the users by the no. of posts they posted, the forum table looks roughly like this: id username 1user1 2user2 3user1 4user1 5user2 The ID doesnt really matter, what I want to do is to organize the users according to the number of posts they have made, so the output looks like: username posts user1 3 user2 2 where left is the username and right is the no of posts Thanks Ted Quote Link to comment https://forums.phpfreaks.com/topic/38280-solved-mysql-complicated-query-help/ Share on other sites More sharing options...
Lodar Posted February 13, 2007 Share Posted February 13, 2007 The quickest way would be to modify the table structure you have there to include a new column called num_posts which is incremented each time the user makes a new post. Then you can order the query results on the num_posts column so your final table structure will look like id username num_posts Its the way I have always done mine, and cuts down on potentially long queries finding all the posts that the user made in the database Quote Link to comment https://forums.phpfreaks.com/topic/38280-solved-mysql-complicated-query-help/#findComment-183420 Share on other sites More sharing options...
o3d Posted February 13, 2007 Share Posted February 13, 2007 This query should do the trick. select username, count(*) as posts from table group by username order by posts desc Quote Link to comment https://forums.phpfreaks.com/topic/38280-solved-mysql-complicated-query-help/#findComment-183423 Share on other sites More sharing options...
fenway Posted February 13, 2007 Share Posted February 13, 2007 The quickest way would be to modify the table structure you have there to include a new column called num_posts which is incremented each time the user makes a new post. Then you can order the query results on the num_posts column so your final table structure will look like id username num_posts Its the way I have always done mine, and cuts down on potentially long queries finding all the posts that the user made in the database This way is quick, but unless you're dealing with InnoDB tables, summary tables/columns are expensive, and prone to errors. Quote Link to comment https://forums.phpfreaks.com/topic/38280-solved-mysql-complicated-query-help/#findComment-183630 Share on other sites More sharing options...
Lodar Posted February 13, 2007 Share Posted February 13, 2007 ok thanks for updating me, its good to know. Quote Link to comment https://forums.phpfreaks.com/topic/38280-solved-mysql-complicated-query-help/#findComment-183646 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.