PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

mysql的Union All如何使用

王林
王林 转载
2023-05-26 20:37:04 2779浏览

一、Union All介绍

对两个结果集进行合并操作,包括重复行数据,不会对两个结果集做任何处理。

使用语法

SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;

注意:UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。

二、使用实例

用union all去除结果后在用distinct排重

-- 用union all去除结果后在用distinct排重,执行时间为:5.4秒
select DISTINCT xx.DO_DETAIL_ID from (
select do_detail_id  from A
union all 
select do_detail_id  from B) xx;

用union all去除结果后在用group by排重

-- 用union all去除结果后在用group by排重,执行时间为:5.69秒
select yy.DO_DETAIL_ID from (
select do_detail_id from A
union all 
select do_detail_id from B) yy GROUP BY yy.DO_DETAIL_ID;
声明:本文转载于:亿速云,如有侵犯,请联系admin@php.cn删除