当前位置:网站首页>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 :
边栏推荐
- Leetcode 1328. 破坏回文串(可以,已解决)
- [binomial tree] the power of the button cattle customers must brush questions
- 【多线程】JUC详解 (Callable接口、RenntrantLock、Semaphore、CountDownLatch) 、线程安全集合类面试题
- Unity高版本退回低版本报错问题
- web安全入门-部署Snort开源IDS/IPS系统
- XSS. haozi. Me brush questions
- Mpu9250 ky9250 attitude, angle module and mpu9250 MPL DMA comparison
- Loj#2324-「清华集训 2017」小 Y 和二叉树
- Tire Defect Detection Using Fully Convolutional Network-论文阅读笔记
- Codeforces - 587e (linear basis + segment tree + difference)
猜你喜欢

Docker install MySQL

LeetCode 745. Prefix and suffix search

Four methods of traversing key value in map

A fastandrobust convolutionalneuralnetwork-based defect detection model inproductqualitycontrol-閱讀筆記

Learning outline of the column "MySQL DBA's magic road"

常用getshell工具的下载

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

The type of MySQL index (single column index, combined index, BTREE index, clustered index, etc.)

每日刷题记录 (二十六)

JVM钩子hooks函数
随机推荐
Keras深度学习实战(14)——从零开始实现R-CNN目标检测
Leetcode 1328. 破坏回文串(可以,已解决)
Ppde Q2 welcome | welcome 22 AI developers to join the propeller developer technical expert program!
At5147-[agc036d]negative cycle [DP, model conversion]
Mysql索引的类型(单列索引、组合索引 btree索引 聚簇索引等)
jconsole线程面板中的阻塞总数和等待总数(转)
TS solves the problem that the type file of the imported plug-in does not exist
A fastandrobust convolutionalneuralnetwork-based defect detection model inproductqualitycontrol-阅读笔记
Huawei machine test: number of continuous licensing
function/symbol ‘pango_context_set_round_glyph_positions‘ not found in library ‘libpango-1.0.so.0‘x
A simple output method of promise object to the result in nodejs (it is recommended to use the asynchronous ultimate scheme async+await)
Docker install MySQL
Leetcode 1310. 子数组异或查询
CodeForces - 587E(线性基+线段树+差分)
委派雙親之類加載器
Unity3d read mpu9250 example source code
Limit query of MySQL optimization series
Daily question brushing record (26)
【多线程】JUC详解 (Callable接口、RenntrantLock、Semaphore、CountDownLatch) 、线程安全集合类面试题
Detailed explanation of multiple linear regression