当前位置:网站首页>SQL union operator
SQL union operator
2022-07-19 11:35:00 【Night drift ice】
SQL UNION Operator ( Union operator )
UNION The operator is used to merge two or more SELECT The result set of the statement .
- UNION Each of the SELECT Statement must have the same number of columns
- These columns are in order , There must be similar data types
UNION grammar
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
UNION ALL grammar
UNION The different values after the combination of the two result sets obtained by the operator . Allow duplicate values , Please use UNION ALL.
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
Be careful : The column names in the result set usually use the first SELECT Column names in statements .
UNION The non repetition of the operator means , Only when the data in a row are different .
Example
Use Microsoft's famous sample database :Northwind.
Customers table One of the select result :
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
Suppliers table One of the select result :
SupplierID | SupplierName | ContactName | Address | City | PostalCode | Country |
1 | Exotic Liquid | Charlotte Cooper | 49 Gilbert St. | London | EC1 4SD | UK |
2 | New Orleans Cajun Delights | Shelley Burke | P.O. Box 78934 | New Orleans | 70117 | USA |
3 | Grandma Kelly's Homestead | Regina Murphy | 707 Oxford Rd. | Ann Arbor | 48104 | USA |
perform UNION Achieve non repetitive results :
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
Use UNION ALL Merge all results , Include repeated :
SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;
add WHERE Clause :
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;
Add one more Type Field , Used to distinguish customer and supplier type :
SELECT 'Customer' AS Type, ContactName, City, Country
FROM Customers
UNION
SELECT 'Supplier', ContactName, City, Country
FROM Suppliers;
Reference resources :
边栏推荐
猜你喜欢

性能优化之@Contended减少伪共享

委派雙親之類加載器

《MySQL DBA封神打怪之路》专栏学习大纲

TCP拥塞控制详解 | 7. 超越TCP

An error, uncaught typeerror: modalfactory is not a constructor

委派双亲之类加载器

E-commerce sales data analysis and prediction (date data statistics, daily statistics, monthly statistics)

Download of common getshell tools

Tier defect detection using full revolutionary network

要想组建敏捷团队,这些方法不可少
随机推荐
02-2、缺省参数、函数重载、引用、隐式类型转换、关于报错
pjudge#21652-[PR #4]到底有没有九【数位dp】
Unity dropdown (editable, inputable) drop-down selection box with Text Association
XSS. haozi. Me brush questions
Resources for physics based simulation in computer graphics
Accident caused by the combination of annotation transaction + distributed lock at the read committed level -- disorder of activity purchase opportunities
委派雙親之類加載器
Hello JSON Schema
Chapter 1 of creating virtual machine (vmvare virtual machine)
Hello JSON Schema
Detailed explanation of multiple linear regression
Model comparison of material inventory management between sap ECC and s4hana material
Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning
Today's sleep quality record 79 points
【二叉树】之力扣牛客必刷题
What happened to cinder in openstack-m
SPI服务发现机制
A simple output method of promise object to the result in nodejs (it is recommended to use the asynchronous ultimate scheme async+await)
Microservice online specification
JVM hook hooks function