MySQL - 对结果集中的某些条件进行最后排序(也许是联合?)
MySQL - Sort certain conditions last in the result set (Unions maybe?)
我有一个特定的查询,我基本上需要获取具有特定 ID 的两行。一开始我用的是where id = x or id = y
。但是我需要在结果集中获取第二个参数 last 。我遇到了 UNIONS,我需要知道 Unions 如何对结果进行排序。
工会会帮助实现我想要的吗?
如果没有,我怎样才能达到预期的效果?
提前致谢。
select *
from your_table
where id in (x, y)
order by id <> x
或
order by case when id = x then 1 else 2 end
我有一个特定的查询,我基本上需要获取具有特定 ID 的两行。一开始我用的是where id = x or id = y
。但是我需要在结果集中获取第二个参数 last 。我遇到了 UNIONS,我需要知道 Unions 如何对结果进行排序。
工会会帮助实现我想要的吗?
如果没有,我怎样才能达到预期的效果?
提前致谢。
select *
from your_table
where id in (x, y)
order by id <> x
或
order by case when id = x then 1 else 2 end