文章目录
环境
- MySQL 8.0.28
- CentOS 7.9
union和union all的区别
二者的区别在于是否去重。
- union:去除重复记录
- union all:只是简单的把两个结果集拼接在一起
例如,要统计两个网站的每天访问量:
sql
select date, sum(amount) as amount from
(
(select date, count(*) as amount from website1
where id = 123
group by date)
union all
(select date, count(*) as amount from website2
where id = 123
group by date)
) as total
group by date
本例中,不能去重(因为可能两个网站的date和amount恰好都一样),所以使用 union all 。
再例如,要统计一个名单,包含身高最高和体重最重的人员信息:
sql
(select name from t1 order by height desc limit 1)
union
(select name from t1 order by weight desc limit 1)
本例中,必须去重(因为只要一个名单),所以用 union 。
总结:
- 使用
union还是union all,基本上是由业务逻辑决定的(感觉union all的使用场景更多)。 - 优先使用
union all,因为它的效率更高。 - 如果需要去重,当然应该用
union,不过如果确定子查询之间(以及子查询内部)没有重复记录,可以用union all(慎用)。
子查询的括号
sql
select * from t1 where id = 1
union all
select * from t2 where id = 2
sql
(select * from t1 where id = 1)
union all
(select * from t2 where id = 2)
这两个SQL效果一样吗?
答案是一样的。
但是,如果子查询有 order by 、 limit 等条件时,必须用括号,否则会被当做全局的。
例如:
sql
select * from t1 where id = 1
union all
select * from t2 where id = 2 limit 1
虽然看上去 limit 1 和第二个子查询在同一行,但它实际上是全局的,所以整个结果里只有一条记录。
再例如:
sql
select * from t1 where id = 1 limit 1
union all
select * from t2 where id = 2
虽然看上去 limit 1 和第一个子查询在同一行,但它实际上是全局的,所以整个SQL语句有语法错误,无法运行。
再例如:
sql
(select * from website1 where score > 0 order by date desc limit 1)
union all
(select * from website2 where score = 0 order by date desc limit 1)
order by score desc limit 1
这是一个正确的SQL语句。
总结:子查询最好用括号括起来,清晰明了。
union整体去重
我们知道,用 union 连接两个子查询时,会去除重复记录,那么问题来了,这里的"去重",只是针对两个子查询之间的重复记录吗?如果一个子查询本身就包含了重复记录,那么 union 之后会去重吗?
答案是,如果一个子查询本身包含了重复记录,那么 union 之后也会去重。
可以想象, union 是先 union all 把结果集拼接起来,然后再整体去重。
排序
以 union all 为例,它只是简单的拼接两个结果集,那么问题来了,如果没有全局的 order by ,那么最终的结果集是有顺序的吗?
答案是,不要依赖隐式排序,虽然在绝大多数情况下,结果集的顺序就是拼接的顺序,但是不能100%保证如此,所以,有需要时还是要用 order by 来保证顺序。
这就像 select * from t1 一样,虽然绝大多数情况下返回的顺序就是记录的自然顺序,但是保险起见,最好还是加上 order by 来保证顺序。