MySQL Crash Course #09# Chapter 17. Combining Queries: UNION
INDEX
UNION Rules
As you can see, unions are very easy to use. But a few rules govern exactly which can be combined:
A UNION must be comprised of two or more SELECT statements, each separated by the keyword UNION (so, if combining four SELECT statements, three UNION keywords would be used).
Each query in a UNION must contain the same columns, expressions, or aggregate functions (although columns need not be listed in the same order).
Column datatypes must be compatible: They need not be the exact same type, but they must be of a type that MySQL can implicitly convert (for example, different numeric types or different date types).
Aside from these basic rules and restrictions, unions can be used for any data retrieval tasks.
WHERE VS. UNION
For the most part, combining two queries to the same table accomplishes the same thing as a single query with multiple WHERE clause conditions. In other words, any SELECT statement with multiple WHERE clauses can also be specified as a combined query, as you'll see in the section that follows. The performance of each of the two techniques, however, can vary based on the queries used. As such, it is always good to experiment to determine which is preferable for specific queries.
UNION VS. UNION ALL
The UNION automatically removes any duplicate rows from the query result set (in other words, it behaves just as multiple WHERE clause conditions in a single SELECT would). Because vendor 1002 creates a product that costs less than 5, that row was returned by both SELECT statements. When the UNION was used, the duplicate row was eliminated.
This is the default behavior of UNION, but you can change this if you so desire. If you do, in fact, want all occurrences of all matches returned, you can use UNION ALL instead of UNION.
Sorting Combined Query Results
SELECT statement output is sorted using the ORDER BY clause. When combining queries with a UNION, only one ORDER BY clause may be used, and it must occur after the final SELECT statement. There is very little point in sorting part of a result set one way and part another way, and so multiple ORDER BY clauses are not allowed.
The following example sorts the results returned by the previously used UNION:
SELECT vend_id, prod_id, prod_price
FROM products
WHERE prod_price <= 5
UNION
SELECT vend_id, prod_id, prod_price
FROM products
WHERE vend_id IN (1001,1002)
ORDER BY vend_id, prod_price;
MySQL Crash Course #09# Chapter 17. Combining Queries: UNION的更多相关文章
- MySQL Crash Course #06# Chapter 13. 14 GROUP BY. 子查询
		
索引 理解 GROUP BY 过滤数据 vs. 过滤分组 GROUP BY 与 ORDER BY 之不成文的规定 子查询 vs. 联表查询 相关子查询和不相关子查询. 增量构造复杂查询 Always ...
 - MySQL Crash Course #05# Chapter 9. 10. 11. 12 正则.函数. API
		
索引 正则表达式:MySQL only supports a small subset of what is supported in most regular expression implemen ...
 - MySQL Crash Course #13# Chapter 21. Creating and Manipulating Tables
		
之前 manipulate 表里的数据,现在则是 manipulate 表本身. INDEX 创建多列构成的主键 自动增长的规定 查看上一次插入的自增 id 尽量用默认值替代 NULL 外键不可以跨引 ...
 - MySQL Crash Course #11# Chapter 20. Updating and Deleting Data
		
INDEX Updating Data The IGNORE Keyword Deleting Data Faster Deletes Guidelines for Updating and Dele ...
 - MySQL Crash Course #10# Chapter 19. Inserting Data
		
INDEX BAD EXAMPLE Improving Overall Performance Inserting Multiple Rows INSTEAD OF Inserting a Singl ...
 - MySQL Crash Course #04# Chapter 7. 8 AND. OR. IN. NOT. LIKE
		
索引 AND. OR 运算顺序 IN Operator VS. OR NOT 在 MySQL 中的表现 LIKE 之注意事项 运用通配符的技巧 Understanding Order of Evalu ...
 - MySQL Crash Course #21# Chapter 29.30. Database Maintenance & Improving Performance
		
终于结束这本书了,最后两章的内容在官方文档中都有详细介绍,简单过一遍.. 首先是数据备份,最简单直接的就是用 mysql 的内置工具 mysqldump MySQL 8.0 Reference Man ...
 - MySQL Crash Course #20# Chapter 28. Managing Security
		
限制用户的操作权限并不是怕有人恶意搞破坏,而是为了减少失误操作的可能性. 详细文档:https://dev.mysql.com/doc/refman/8.0/en/user-account-manag ...
 - MySQL Crash Course #18# Chapter 26. Managing Transaction Processing
		
InnoDB 支持 transaction ,MyISAM 不支持. 索引: Changing the Default Commit Behavior SAVEPOINT 与 ROLLBACK TO ...
 
随机推荐
- react封装简单的浏览器顶部加载进度条全局组件
			
在项目中经常会有在请求前后加loading或者加加载进度条,一般这些组件都会抽离出来作为全局组件 进度条的插件貌似都不是很符合自己项目中的需求,于是.. 参考nprogress样式,自己在项目中封装组 ...
 - PyQT5-QCalendarWidget 日历显示
			
""" QCalendarWidget:提供了日历插件 Author:dengyexun DateTime:2018.11.22 """ f ...
 - SQL Server查询时添加一列连续的自增列
			
SQL Server查询时添加一列连续的自增列 在SQL Server数据库中表信息会用到Identity关键字来设置自增列.但是当有数据被删除的话,自增列就不连续了.如果想查询出这个表的信息,并添加 ...
 - 解决下载的CHM文件无法显示网页问题
			
问题症状:打开CHM文件,左边目录齐全,可右边边框里却是无法显示网页. 解决方法:方法一:修改注册表1)新建一个文本文件2)添加如下内容:REGEDIT4[HKEY_LOCAL_MACHINE\SOF ...
 - 2018/03/25 每日一个Linux命令 之 df
			
Linux df命令用于显示目前在Linux系统上的文件系统的磁盘使用情况统计. 就像在windows下打开我的电脑一样会统计各个磁盘一样的情况 主要用于查看磁盘空间占用情况 -- [@hong:~] ...
 - paas平台
			
paas平台 定义:PaaS是云计算中重要的一类服务,为用户提供应用的全生命周期管理和相关的资源服务.通过PaaS,用户可以完成应用的构建.部署.运维管理,而不需要自己去搭建计算环境,如安装服务器.操 ...
 - ChinaTest测试感悟
			
这次去北京参加ChinaTest大会,听了各位大师和同行的心得和感悟,收获颇多.很喜欢这样的大会,可以听到测试的各种声音各种观点.当没有对错时,需要思考的就是怎样采取最适合当前环境的策略.言归正传,谈 ...
 - Why String is Immutable or Final in Java
			
The string is Immutable in Java because String objects are cached in String pool. Since cached Strin ...
 - 怎样知道 CPU 是否支持虚拟化技术(VT) | Linux 中国
			
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/F8qG7f9YD02Pe/article/details/79832475 wx_fmt=png&a ...
 - ListView多种item注意以及自己出现的莫名其妙的错误
			
如果ListView不懂,请绕路 1.ListView添加多个item必须用到的两个方法 getViewTypeCount一共有多少种item,我这里写的两种 getItemViewType当前pos ...