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.

有帮助吗?

解决方案

You can first sort by Votes DESC and then Date DESC

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

Fiddle Demo

其他提示

Please try this..

SELECT * FROM TABLE
ORDER BY Votes DESC, DATE DESC

This should work..

SELECT id
       ,Date
       ,Comment
       ,Votes
ORDER BY Votes DESC
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top