How can I order my resultset but also have a specific row (with fieldX = Y
) be the first one?
views:
21answers:
2
+2
A:
Something like this should work:
ORDER BY (fieldX = Y) DESC, whateverField ASC
Ike Walker
2010-08-16 13:15:36
+2
A:
select tbl.*, if(fieldX = Y, 1, 0) as custom_sort
from tbl
order by custom_sort desc, fieldZ asc
Now whatever the fieldZ may be, the row with fieldX = Y will always be the first one in the resultset
ovais.tariq
2010-08-16 17:35:05
that would work too, but i feel like Ike's solution is cleaner for this purpose.
Garrett
2010-08-16 17:54:51