我有 SELECT
查询 LEFT JOINT (SELECT)
并按 COALESCE(SELECT2.date, SELECT1.date)
排序,但我需要也可以使用 important
字段对其进行排序。
因此:如果行有 important = 1
,则它需要排在第一位,然后是 important = 0
但按合并日期排序。这甚至可能吗?
最佳答案
您可以将 important
列放在 order by
列表的第一位。排序 desc
(降序)会将 1 放在 0 之前:
order by
important DESC
, coalesce(select2.date, select1.date)
您甚至可以使用 case
以自定义方式对 important
列进行排序:
order by
case
when important = 'SENATOR' then 1
when important = 'PATRICIAN' then 2
when important = 'PLEBS' then 3
else 4
end
, coalesce(select2.date, select1.date)
关于mysql - 按日期和字段排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28303044/