I have a table consisting of: elements, ranking ranking and the number of votes that the user imposed on it. For simplicity: id, ranks, voices.
I am trying to run a query that sorts items from the highest score to the lowest if the number of votes = 0.
Since I have a base ranking score for new items, ordering my rating is not possible.
I tried the following, along with some other permutations:
$list = $dbh->query('SELECT * FROM ranks ORDER BY votes desc, rating desc case when min(votes)= 0 else rating desc');
to no avail by looking: Conditional sorting in MySQL and Conditional sorting in MySQL?
I tried this:
SELECT * FROM ranks ORDER BY rating desc, case votes when 0 then votes end
This is the table and the output I get:
ID : Rating : Votes 2 : 201 : 9 3 : 100 : 0 4 : 100 : 0 5 : 100 : 0 1 : -13 : 9
My perfect way out:
ID : Rating : Votes 2 : 201 : 9 1 : -13 : 9 4 : 100 : 0 5 : 100 : 0 3 : 100 : 0
As you can see, this does not group all 0 voted items to the end.
I am sure that this forehead is respectfully simple, I would really appreciate any pointers and best practices. Hooray!
source share