Question

I am developing a mysql based commenting system in which I have to sort the comments by Popular, Newest and oldest, I have the newest and oldest working properly but I am having trouble in sorting the comments by Popularity. I have a comments table in my database which stores the comment_date, comments_votes and comment_text, in popular I want to first sort the comments by votes, and for the comments which don't have any votes I want to sort them by date.

For Example If I have

 id   |   Date   |   Comment   | Votes
 1    | 24 March |    Hello    |  0
 2    | 23 March |    WORLD    |  1
 3    | 25 March |    Hello    |  0
 4    | 26 March |    Hello    |  2

I want it to sort so that the result is returned in this order 4, 2, 3, 1.

Était-ce utile?

La solution

You can first sort by Votes DESC and then Date DESC

select * from Table1
ORDER BY Votes DESC ,`Date` DESC

Fiddle Demo

Autres conseils

Please try this..

SELECT * FROM TABLE
ORDER BY Votes DESC, DATE DESC

This should work..

SELECT id
       ,Date
       ,Comment
       ,Votes
ORDER BY Votes DESC
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top