mysql group by 不改变union all中列表的顺序

标签 mysql group-by sql-order-by

这是我的查询

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/

相关文章:

mysql - 查询上一行值并合并到当前行

php - 如何在 PHP 和 MySQL 中操作数组?

MYSQL 按列分组,每组 2 行

PHP: Orderby "ID"foreach

SQL 服务器 2000 : Length of field in 1/3 (or 2/3) of the records

sql - OVER ORDER BY 中的多个列

即使没有匹配项,Mysql REGEXP 也会返回 true。

php - 在存储在数据库中的序列化值中搜索

sql - 捕获前 5 行并组合其余的 SQL Server

mysql - MySQL中指定GROUP BY