两种方式:
第一种:
先各自排序,然后通过临时表嵌套再合并结果,注意排序后面必须加入 limit,否则order by不起作用
SELECT * FROM (SELECT * FROM t1 WHERE id IN (1,3,6) ORDER BY utime DESC limit 5) AS a
UNION ALL
SELECT * FROM (SELECT * FROM t1 WHERE id IN (2,4,5) ORDER BY utime DESC limit 5) AS b
;
第二种:
通过加入临时列px查出所有结果后,再按px,utime排序
SELECT * FROM(
SELECT 0 AS px,id,NAME,utime FROM (SELECT * FROM t1 WHERE id IN (1,3,6)) AS a
UNION ALL
SELECT 1 AS px,id,NAME,utime FROM (SELECT * FROM t1 WHERE id IN (2,4,5) ) AS b
) AS c ORDER BY px,utime DESC
时间: 2024-10-24 16:21:23