union和union all区别
UNION
会把两个查询语句的结果集合并起来。
union
会过滤掉两个结果集中重复的行,而union all
不会过滤掉重复行。
union order
在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;