MySQL:ORDER BY whateverField ASC [首先放行X]?

时间:2010-08-16 13:11:20

标签: mysql sql-order-by

如何订购我的结果集,但是还有一个特定的行(fieldX = Y)是第一行?

2 个答案:

答案 0 :(得分:3)

这样的事情应该有效:

ORDER BY (fieldX = Y) DESC, whateverField ASC

答案 1 :(得分:2)

select tbl.*, if(fieldX = Y, 1, 0) as custom_sort 
from tbl
order by custom_sort desc, fieldZ asc

现在无论fieldZ是什么,fieldX = Y的行将始终是结果集中的第一行