Sort MySQL query with multiple joined tables

To put 237 first, you can use:

ORDER BY (values.attributeId = 237) DESC
         objects.id asc

This uses the MySQL feature that boolean expressions are treated as numbers in a numeric context, with “1” for true and “0” for false (that is why DESC is needed for this to be first).

This is equivalent to:

ORDER BY (CASE WHEN values.attributeId = 237 THEN 1 ELSE 0 END) DESC
         objects.id asc

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top