Selecting DIFFERENT rows in PHP
Posted: Thu May 11, 2006 2:14 pm
Hi,
I am displaying results of a mysql query that shows the last 5 stories visitors have commented on. The comments table has an ID field which obviousley increases as the more comments are posted. So, I decided to do something simple and just ORDER BY id DESC. The problem is, if 2 visitors commented on the same story right after each other, its going to display the same story twice. How do i make it so that PHP selects 5 different rows, which have the highest amount of comments on them?
For example : Comments Table
ID - 1 Title - Story 1
ID - 2 Title - Story 2
ID - 3 Title - Story 2
ID - 4 Title - Story 3
ID - 5 Title - Story 4
ID - 6 Title - Story 4
ID - 7 Title - Story 5
I want it to display as :
Active Stories :
Story 5
Story 4
Story 3
Story 2
Story 1
If I just use a simple ORDER BY id query, it will display as :
Active Stories :
Story 5
Story 4
Story 4
Story 3
Story 2
I do not want that.
Thanks!
I am displaying results of a mysql query that shows the last 5 stories visitors have commented on. The comments table has an ID field which obviousley increases as the more comments are posted. So, I decided to do something simple and just ORDER BY id DESC. The problem is, if 2 visitors commented on the same story right after each other, its going to display the same story twice. How do i make it so that PHP selects 5 different rows, which have the highest amount of comments on them?
For example : Comments Table
ID - 1 Title - Story 1
ID - 2 Title - Story 2
ID - 3 Title - Story 2
ID - 4 Title - Story 3
ID - 5 Title - Story 4
ID - 6 Title - Story 4
ID - 7 Title - Story 5
I want it to display as :
Active Stories :
Story 5
Story 4
Story 3
Story 2
Story 1
If I just use a simple ORDER BY id query, it will display as :
Active Stories :
Story 5
Story 4
Story 4
Story 3
Story 2
I do not want that.
Thanks!