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!
Selecting DIFFERENT rows in PHP
Moderator: General Moderators
Please, get yourself an SQL manual and find out the differences... Since they are quite significant...
DISTINCT will remove duplicate rows from a resultset (So first you need to lookup when two rows are equal. Notice that DISTINCT applies to all the columns in the SELECT query and not only the column right after DISTINCT).
When you GROUP BY you get 'sets/groups of rows' in the resultset.
DISTINCT will remove duplicate rows from a resultset (So first you need to lookup when two rows are equal. Notice that DISTINCT applies to all the columns in the SELECT query and not only the column right after DISTINCT).
When you GROUP BY you get 'sets/groups of rows' in the resultset.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
You could always google 'SELECT DISTINCT queries' and try something first. A lot of people around here are more than willing to help you after you have tried and failed. Few will do it for you.yoey2000 wrote:Hi,
What kind of code could I use to group by the title or by distinct?