当前位置:网站首页>SQL UNION操作符
SQL UNION操作符
2022-07-17 14:23:00 【夜流冰】
SQL UNION Operator (联合操作符)
UNION操作符用于合并两个或多个SELECT语句的结果集。
- UNION中的每个SELECT语句必须有相同数量的列
- 这些列按照顺序,必须有类似的数据类型
UNION语法
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
UNION ALL语法
UNION操作符得到的两个结果集组合后的各个不同的值。要允许重复的值,请使用UNION ALL。
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
注意:结果集中的列名通常使用第一个SELECT语句中的列名。
UNION操作符的不重复的意思是,一行的数据都不相同才算不同。
示例
使用微软的有名的样本数据库:Northwind。
Customers table的一个select结果:
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的一个select结果:
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 |
执行UNION取得不重复的结果:
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
使用UNION ALL合并所有结果,包括重复的:
SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;
加上WHERE子句:
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;
增加一个Type字段,用来区分customer和supplier类型:
SELECT 'Customer' AS Type, ContactName, City, Country
FROM Customers
UNION
SELECT 'Supplier', ContactName, City, Country
FROM Suppliers;
参考:
边栏推荐
- Category imbalance in classification tasks
- Ppde Q2 welcome | welcome 22 AI developers to join the propeller developer technical expert program!
- [handwritten numeral recognition] handwritten numeral recognition based on lenet network with matlab code
- Tier defect detection using full revolutionary network
- How does unity3d use the asset store to download some useful resource packages
- Qt--优秀开源项目
- Daily question brushing record (26)
- SSM uses POI to export data to excel
- Maximal semi connected subgraph (tarjan contraction + topological ordering + DP longest chain)
- JVM钩子hooks函数
猜你喜欢

Google Earth engine - Hansen global forest change v1.8 (2000-2020) forest coverage and forest loss data set

To build agile teams, these methods are indispensable

XSS.haozi.me刷题

Déléguer un chargeur tel qu'un parent

Pytoch and weight decay (L2 norm)

Documents required for military product development process - advanced version

466-82(3、146、215)

Conversion of unity3d model center point (source code)

LeetCode 558. 四叉树交集

Deep learning for generic object detection: a survey
随机推荐
Tire Defect Detection Using Fully Convolutional Network-论文阅读笔记
华为无线设备配置频谱导航
mpu9250 ky9250姿态、角度模块和mpu9250 mpl dma对比
JVM钩子hooks函数
The case has been solved --- no matter how to change the code from the logic of MQ consumption, it will not take effect
Discussion on Euler angle solution of rocket large maneuvering motion
PPDE第二季度迎新 | 欢迎22位AI开发者加入飞桨开发者技术专家计划!
SSM uses POI to export data to excel
《MySQL DBA封神打怪之路》专栏学习大纲
Maximal semi connected subgraph (tarjan contraction + topological ordering + DP longest chain)
机器人开发--机器人资料汇总
SAP S4 material management inventory module mard database table reading technical details
XSS. haozi. Me brush questions
synchronized锁升级
CodeForces - 587E(线性基+线段树+差分)
STC8H开发(十四): I2C驱动RX8025T高精度实时时钟芯片
Deep learning for generic object detection: a survey
Qt--优秀开源项目
MySQL autoincrement ID, UUID and snowflake ID
2022/7/15