mysql union order by 分别排序问题
两种方式:
第一种:
先各自排序,然后通过临时表嵌套再合并结果,注意排序后面必须加入 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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。