I have a query to fetch the top 50 entries based on a total_score field. From these top 50, I need to be able to order them by any of their other fields. I tried using multiple ORDER BY statements, but the result set never changes. Here’s my query:
SELECT
e.id, e.name , e.total_score
FROM
entry e
ORDER BY
e.total_score DESC, e.name ASC
I think I understand why this doesn’t do what I need, so I suppose my question is how can I achieve my goal? In the end I always need the 50 entries with the highest total_score, but this selection should be then orderable by any other field.
If this isn’t possible in MySQL, I can do it with PHP, but I’d rather let the DB handle this.
Note: I’m using Doctrine 1.2 on top of a MySQL 5 DB.
Thanks!
ORDER BY, when used with multiple columns, only uses the subsequent columns in case of tie in the previous ones.
Try using nested queries