Home  >  Article  >  Database  >  How to use Union All of mysql

How to use Union All of mysql

王林
王林forward
2023-05-26 20:37:043121browse

1. Introduction to Union All

Merges two result sets, including duplicate row data, without any processing of the two result sets.

Using syntax

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

Note: The column names in the UNION result set are always equal to the column names in the first SELECT statement in UNION.

2. Usage examples

Use union all to remove the results and then use distinct to eliminate duplicates

-- 用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;

Use After union all removes the results, use group by to sort out the duplicates

-- 用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;

The above is the detailed content of How to use Union All of mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete