创建一个测试数据表

CREATE TABLE `temp_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`endtime` int(10) NOT NULL,
`basic_sort` smallint(8) NOT NULL,
`type` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

对UNION 和UNION ALL 进行比较:

在MySQL 5.1中文手册中有下面一句话:

如果您对UNION不使用关键词ALL,则所有返回的行都是唯一的,如同您已经对整个结果集合使用了DISTINCT。如果您指定了ALL,您会从所有用过的SELECT语句中得到所有匹配的行。

插入测试数据:

INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('a', '1534349307', '10', '2');
INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('a', '1534349308', '14', '2');
INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('c', '1534349309', '12', '1');
INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('d', '1534349310', '12', '1');

(SELECT id,name,endtime,basic_sort,type from temp_table where type=1)
UNION
(SELECT id,name,endtime,basic_sort,type from temp_table where type=2);
(SELECT id,name,endtime,basic_sort,type from temp_table where type=1)
UNION ALL
(SELECT id,name,endtime,basic_sort,type from temp_table where type=2);

对于上面两句分别是UNION和UNION ALL组合,但是得到的结果是一致的。

其实上面说的【所有返回的行都是唯一的】这里所说的是主键唯一:

(SELECT id,name,endtime,basic_sort,type from temp_table)
UNION
(SELECT id,name,endtime,basic_sort,type from temp_table);

(SELECT id,name,endtime,basic_sort,type from temp_table)
UNION ALL
(SELECT id,name,endtime,basic_sort,type from temp_table);

这里对于UNION返回的结果相当于对主键做一次DISTINCT,而UNION ALL 返回的结果相当于是单纯把结果查询出来然后合并返回。

对UNION查询结果进行排序:

插入测试数据(将刚刚的表内容删减,使用TRUNCATE `temp_table`):

INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('a', '1534349307', '10', '2');
INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('b', '1534349308', '14', '2');
INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('c', '1534349309', '12', '2');
INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('d', '1534349310', '12', '2');
INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('e', '1534349311', '18', '2');
INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('f', '1534349312', '18', '2');
INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('aa', '1534349313', '11', '1');
INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('bb', '1534349314', '11', '1');
INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('cc', '1534349315', '13', '1');
INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('dd', '1534349316', '12', '1');
INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('ee', '1534349317', '12', '1');
INSERT INTO `temp_table` (`name`,`endtime`,`basic_sort`,`type`) VALUES ('ff', '1534349318', '15', '1');

这里我们想要得到的结果是:按type的正序排序,接着按basic_sort倒叙排序

(select * from temp_table where type=1 ORDER BY basic_sort desc)
union
(select * from temp_table where type=2 ORDER BY basic_sort desc)

对于这样的SQL语句得到的结果是:

与没有在子句中使用ORDER BY的结果一致。

(select * from temp_table where type=1) union (select * from temp_table where type=2)

在MySQL 5.1中文手册中有下面两句话:

1、如果您想使用ORDER BY或LIMIT子句来对全部UNION结果进行分类或限制,则应对单个地SELECT语句加圆括号,并把ORDER BY或LIMIT放到最后一个的后面。

如果要完成上面的需求应该是:

(select *,type as sort_type,basic_sort as sort_value from temp_table)
union
(select *,type as sort_type,basic_sort as sort_value from temp_table)
order by sort_type ASC,sort_value ASC

2、圆括号中用于单个SELECT语句的ORDER BY只有当与LIMIT结合后,才起作用。否则,ORDER BY被优化去除。

(select * from temp_table WHERE type=1 ORDER BY basic_sort ASC LIMIT 6)
union
(select * from temp_table WHERE type=2 ORDER BY basic_sort ASC LIMIT 6)

关于这里是可以使用ORDER BY 与LIMIT 共同完成,但是有些时候我们不能在里面用LIMIT 只能在外面用,比如我们做分页,我们并不知道应该在里面LIMIT 多少才对。

下面这里有个奇怪的需求,我们需要得到下面一个这个列表,首先是按type正序排列,当type为1的时候是按照basic_sort进行正序排列,当type为2的时候按endtime倒叙排列。

因为对于type为1的时候我们不需要对endtime进行排序所以我们在type为1的时候设置endtime的值为0,对于type为2的时候我们不需要basic_sort的值进行排序,所以设置basic_sort的值为0。

(SELECT id,name,endtime,basic_sort,basic_sort as sort_basic_sort,0 as sort_endtime,type,type as sort_type FROM temp_table WHERE type=1)
union
(SELECT id,name,endtime,basic_sort,0 as sort_basic_sort,endtime as sort_endtime,type,type as sort_type FROM temp_table WHERE type=2)
ORDER BY sort_type ASC,sort_basic_sort ASC,sort_endtime DESC

关于union的一些问题的更多相关文章

  1. SQL Server-聚焦UNIOL ALL/UNION查询(二十三)

    前言 本节我们来看看有关查询中UNION和UNION ALL的问题,简短的内容,深入的理解,Always to review the basics. 初探UNION和UNION ALL 首先我们过一遍 ...

  2. SQL 提示介绍 hash/merge/concat union

    查询提示一直是个很有争议的东西,因为他影响了sql server 自己选择执行计划.很多人在问是否应该使用查询提示的时候一般会被告知慎用或不要使用...但是个人认为善用提示在不修改语句的条件下,是常用 ...

  3. LINQ to SQL语句(8)之Concat/Union/Intersect/Except

    适用场景:对两个集合的处理,例如追加.合并.取相同项.相交项等等. Concat(连接) 说明:连接不同的集合,不会自动过滤相同项:延迟. 1.简单形式: var q = ( from c in db ...

  4. SQLServer-----Union,Union All的使用方法

    转载: http://blog.csdn.net/kiqinie/article/details/8132485 select a.Name from Material as a union sele ...

  5. 假如 UNION ALL 里面的子句 有 JOIN ,那个执行更快呢

    比如: select id, name from table1 where name = 'x' union all select id, name from table2 where name =  ...

  6. sql union和union all的用法及效率

    UNION指令的目的是将两个SQL语句的结果合并起来.从这个角度来看, 我们会产生这样的感觉,UNION跟JOIN似乎有些许类似,因为这两个指令都可以由多个表格中撷取资料. UNION的一个限制是两个 ...

  7. 【oracle】union、union all、intersect、minus 的用法及区别

    一.union与union all 首先建两个view create or replace view test_view_1 as as c from dual union as c from dua ...

  8. sql with as union all

    WITH RPL (FId,Fname,Forder) AS ( SELECT ment.deptno,ment.deptname,ment.orderno FROM JTERP..fg_depart ...

  9. Oracle 中 union 和union all 的简单使用说明

    1.刚刚工作不久,经常接触oracle,但是对oracle很多东西都不是很熟.今天我们来了解一下union和union all的简单使用说明.Union(union all): 指令的目的是将两个 S ...

  10. LINQ系列:LINQ to SQL Concat/Union

    1. Concat 单列Concat var expr = (from p in context.Products select p.ProductName) .Concat( from c in c ...

随机推荐

  1. Trie UVALive 7192 Chip Factory (15长春J)

    题目传送门 题意:从n个数中选出不同的三个数a b c,使得(a+b)^c 最大 分析:先将所有数字按位插入到字典树上,然后删除两个数字,贪心询问与剩下的数字最大异或值. /************* ...

  2. 自定义xml spring bean

    一. xml中bean解析过程 扫描META-INF下面的 spring.schemas  bean定义对应的xsd位置,在IDEA中可以辅助校验) spring.handlers   xmlns对应 ...

  3. Linux利用iptables实现真-全局代理

    对于经常要浏览油管等被墙网站的人而言,利用代理来实现fq是非常有必要的.现在fq的方法中,最为主流的应该要数ssr了,因此本教程都是基于ssr的socks5代理而言的. 在windows中,ssr客户 ...

  4. Asp.net中的ViewState用法

    Session,ViewState用法基本理论:session值是保存在服务器内存上,那么,可以肯定,大量的使用session将导致服务器负担加重. 而viewstate由于只是将数据存入到页面隐藏控 ...

  5. Unity C# 调用SaveFileDialog保存Excel文件

    本文原创,转载请注明出处:http://www.cnblogs.com/AdvancePikachu/p/6893934.html 本文学习如何把数据转存为Excel文件并调用SaveFileDial ...

  6. 处理 wait millis 60009, active 50 ,maxactive 200 异常 过程

    处理 wait millis 60009, active 50 ,maxactive 200 异常 过程2018年04月19日 16:48:46 守望dfdfdf 阅读数:1910 标签: druid ...

  7. Quartz 定时器,同时运用多个定时器

    效果:每天执行两个定时器,两个定时器不相关联.jar版本Quartz 2.2.3 Java工程结构图  jar 包下载: 链接: https://pan.baidu.com/s/1-7dh620k9P ...

  8. [LeetCode]9. Palindrome Number回文数

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  9. yum 和 rpm安装mysql彻底删除

    1.yum方式安装的MySQL $ yum remove mysql mysql-server mysql-libs compat-mysql51 $ rm -rf /var/lib/mysq $ r ...

  10. python全栈测试题(一)

    1.执行Python脚本的两种方式 如果想要永久保存代码,就要用文件的方式 如果想要调试代码,就要用交互式的方式即终端命令下和python环境中 2.Pyhton单行注释和多行注释分别用什么? pyt ...