union和union all区别
UNION会把两个查询语句的结果集合并起来。
union会过滤掉两个结果集中重复的行,而union all不会过滤掉重复行。
union / order by
在union两个带order的select的时候,会报错。
ERROR 1221 (HY000): Incorrect usage of UNION and ORDER BY
解决方法就是使用括号,把两个SELECT括起来。
例如:
(SELECT A,B,C FROM `s` ORDER BY A DESC)
UNION
(SELECT A,B,C FROM `r` ORDER BY B DESC);
或用SELECT AS TABLE
SELECT * FROM
(SELECT A,B,C FROM `s` ORDER BY A DESC)
AS t1
UNION
SELECT A,B,C FROM `r` ORDER BY B DESC;
————————————————
版权声明:本文为CSDN博主「小龙在山东」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lilongsy/article/details/84969097