这是我的查询
select id from ((select id from users where id = 2
)
union all
(select id from users where id = 1
)
union all
(select id from users where id = 1
)
union all
(select id from users where id = 4
)
union all
(select id from users where id = 3
)
)results group by id
我得到的输出是
id
----
1
2
3
4
我期望的输出是
id
----
2
1
4
3
有什么解决办法吗?
我需要按 id 分组的结果,但不需要有序列表。
最佳答案
看来你需要这样的东西不是全部联合
SELECT `id` FROM `users` WHERE id IN(1,2,3,4);
关于mysql group by 不改变union all中列表的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45480292/