Jump to content

[SOLVED] mysql complicated query help:


ted_chou12

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.