当前位置:网站首页>What is the difference between union and union all

What is the difference between union and union all

2022-06-12 19:50:00 WD Technology

UNION The operator is used to merge two or more SELECT The result set of the statement .
Please note that ,UNION Inside SELECT Statements must have the same number of columns . Columns must also have similar data types . meanwhile , Every one of them SELECT The order of the columns in the statement must be the same .

Union: Combine two result sets , Do not include repeating lines , At the same time, sort the default rules ;
UNION Duplicate records will be filtered out after table linking , Therefore, after the table is linked, the generated result set will be sorted , Delete duplicate records and return results .
In practice, most applications will not produce duplicate records , The most common are process tables and history tables UNION

Union All: Combine two result sets , Include repeating lines , No sorting ;
If there are duplicate data in two returned result sets , Then the returned result set will contain duplicate data

example
 Insert picture description here
The query results of the two tables are as follows
 Insert picture description here
It's easy to see , Among the test data inserted above , One is repeated

Then we Let's look at the execution first union have a look
 Insert picture description here
Re execution union all have a look
 Insert picture description here

原网站

版权声明
本文为[WD Technology]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202281534381993.html