I am implementing a Popular feature into my app where I will select the most popular posts from a database. Problem is I need to get the most popular in terms of most likes and most comments. I just tried:
SELECT * FROM db ORDER BY `likesCount`,`commentsCount` DESC LIMIT $from," . ($to-$from)
But that will show an ascending value in terms of the numberOfLikes. How can I get it so that it measures the numberOfLikes and numberOfComments and selects the most popular one?
You can specify whether you want to sort in ascending or descending order for each search field.
With that said, you won’t be able to select the most like and most commented ones at the same time. You’ll have to come up with a formula to figure that out. For instance, the example below find the maximum value for a given “id” between the number of likes and comments:
Another alternative would be to add them together: