当前位置:网站首页>11、 学习MySQL UNION 操作符
11、 学习MySQL UNION 操作符
2022-07-28 17:44:00 【微服务 spring cloud】
MySQL UNION 操作符
本教程为大家介绍 MySQL UNION 操作符的语法和实例。
描述
MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据。

语法
MySQL UNION 操作符语法格式:
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION [ALL | DISTINCT]
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];参数
expression1, expression2, ... expression_n: 要检索的列。
tables: 要检索的数据表。
WHERE conditions: 可选, 检索条件。
DISTINCT: 可选,删除结果集中重复的数据。默认情况下 UNION 操作符已经删除了重复数据,所以 DISTINCT 修饰符对结果没啥影响。
ALL: 可选,返回所有结果集,包含重复数据。
演示数据库
在本教程中,我们将使用 XXXXXX 样本数据库。
下面是选自 "Websites" 表的数据:
mysql> SELECT * FROM Websites;
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | xx教程 | http://www.xxxxxx.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+下面是 "apps" APP 的数据:
mysql> SELECT * FROM apps;
+----+------------+-------------------------+---------+
| id | app_name | url | country |
+----+------------+-------------------------+---------+
| 1 | QQ APP | http://im.qq.com/ | CN |
| 2 | 微博 APP | http://weibo.com/ | CN |
| 3 | 淘宝 APP | https://www.taobao.com/ | CN |
+----+------------+-------------------------+---------+
3 rows in set (0.00 sec)SQL UNION 实例
下面的 SQL 语句从 "Websites" 和 "apps" 表中选取所有不同的country(只有不同的值):
实例
SELECT country FROM Websites
UNION
SELECT country FROM apps
ORDER BY country;执行以上 SQL 输出结果如下:

注释:UNION 不能用于列出两个表中所有的country。如果一些网站和APP来自同一个国家,每个国家只会列出一次。UNION 只会选取不同的值。请使用 UNION ALL 来选取重复的值!
SQL UNION ALL 实例
下面的 SQL 语句使用 UNION ALL 从 "Websites" 和 "apps" 表中选取所有的country(也有重复的值):
实例
SELECT country FROM Websites
UNION ALL
SELECT country FROM apps
ORDER BY country;执行以上 SQL 输出结果如下:

带有 WHERE 的 SQL UNION ALL
下面的 SQL 语句使用 UNION ALL 从 "Websites" 和 "apps" 表中选取所有的中国(CN)的数据(也有重复的值):
实例
SELECT country, name FROM Websites
WHERE country='CN'
UNION ALL
SELECT country, app_name FROM apps
WHERE country='CN'
ORDER BY country;执行以上 SQL 输出结果如下:

边栏推荐
- Adobe Flash player 34.0.0.92 and available version modification methods (2021-01-23
- Srs4.0 installation steps
- R语言与数据分析实战11-数据的删除
- Application of time series database in cigarette factory
- OpenOCD如何通过stlink直接下载程序到stm32板子(已解决)
- 诺基亚扩大与英国电信的5G协议,将成其最大无线接入设备供应商
- SaltStack之salt-ssh
- NDK 系列(5):JNI 从入门到实践,爆肝万字详解!
- JDBC simple encapsulation
- 使用SaltStack自动化部署LNMP
猜你喜欢

Srs4.0 installation steps

Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation

Pytoch: implementation of crossentropyloss and labelsmoothing

Nips18(AD) - 利用几何增广的无监督异常检测《Deep Anomaly Detection Using Geometric Transformations》

Asp net MVC web development tutorial

Image processing web application development tutorial

使用SaltStack自动化部署LNMP
![[solved] ac86u ml revision firmware virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements](/img/1c/6dbfcb5e6ade52d8cbfabcb34616a5.png)
[solved] ac86u ml revision firmware virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements

BLDC 6步换相 simulink

Streamlit machine learning application development tutorial
随机推荐
BLDC 6-step commutation simulink
[notes] Apocalypse: list of practical experience and reflection of product managers
[solved] ac86u ml revision firmware virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements
JS 批量添加事件监听onclick this 事件委托 target currentTarget onmouseenter onmouseover
Smart contract security - overflow vulnerability
source insight项目导入和使用教程
C语言循环语句强化练习题
Test Development Notes
navicate修改数据库名的方式
可转债概念表 x Notion 给你方便快捷的体验!
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
SaltStack之数据系统
Pytorch:交叉熵损失(CrossEntropyLoss)以及标签平滑(LabelSmoothing)的实现
lua语言的左对齐函数(手写)
微信公众号授权登录后报redirect_uri参数错误的问题
C string to short[] method
Kotlin Android development novice tutorial
IMU heating
这种动态规划你见过吗——状态机动态规划之股票问题(下)
我的第二次博客——C语言