Home > Database > Mysql Tutorial > body text

How to use Union All of mysql

王林
Release: 2023-05-26 20:37:04
forward
3126 people have browsed it

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;
Copy after login

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;
Copy after login

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;
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template