union的用法
union的用法
union用来连接两个查询语句,把两个查询语句的查询结果合并起来,两个查询语句的查询字段个数必须一样,否则会出错,查询的字段可以不一样,类型也可以不一样,但是这样查询的意义不大,如果查询的字段不一样,最终的结果集以前者查询的字段为准。如果用union进行连接,碰到所有字段值一样的列,就会合并,去掉重复的行,如果用union all进行连接,则不会去掉重复的内容,所有的内容都被取出。 mysql> (select goods_id,goods_name,cat_id from goods where cat_id=2 order by goo
ds_id asc)
-> union (select goods_id,goods_name,cat_id from goods where cat_id=4 order
by goods_id desc);
+----------+--------------+--------+
| goods_id | goods_name | cat_id |
+----------+--------------+--------+
| 16 | 恒基伟业G101 | 2 |
| 1 | KD876 | 4 |
| 14 | 诺基亚5800XM | 4 |
| 18 | 夏新T5 | 4 |
+----------+--------------+--------+
4 rows in set (0.00 sec)
#在每个查询语句中进行排序,再进行连接,其中的排序就没有意义,所以mysql回把排序的语句进行优化掉 mysql> select goods_id,cat_id,goods_name from goods where cat_id=2
-> union
-> select goods_id,cat_id,goods_name from goods where cat_id=4
-> order by goods_id;
+----------+--------+--------------+
| goods_id | cat_id | goods_name |
+----------+--------+--------------+
| 1 | 4 | KD876 |
| 14 | 4 | 诺基亚5800XM |
| 16 | 2 | 恒基伟业G101 |
| 18 | 4 | 夏新T5 |
+----------+--------+--------------+
4 rows in set (0.00 sec) mysql> #排序放在最后是针对最后的结果集进行排序,有意义, mysql> (select goods_id,cat_id,goods_name from goods where cat_id=3 order by goo
ds_id limit 3)
-> union
-> (select goods_id,cat_id,goods_name from goods where cat_id=4 order by goo
ds_id limit 2)
-> ;
+----------+--------+--------------+
| goods_id | cat_id | goods_name |
+----------+--------+--------------+
| 8 | 3 | 飞利浦9@9v |
| 9 | 3 | 诺基亚E66 |
| 10 | 3 | 索爱C702c |
| 1 | 4 | KD876 |
| 14 | 4 | 诺基亚5800XM |
+----------+--------+--------------+
5 rows in set (0.05 sec) #这次排序影响的最终的显示结果有意义,所以order by 起了作用 一道面试题
mysql> create table a (
-> id char(1),
-> num int
-> )engine myisam charset utf8;
Query OK, 0 rows affected (0.12 sec) mysql> insert into a values ('a',5),('b',10),('c',15),('d',10);
Query OK, 4 rows affected (0.03 sec)
Records: 4 Duplicates: 0 Warnings: 0 mysql> create table b (
-> id char(1),
-> num int
-> )engine myisam charset utf8;
Query OK, 0 rows affected (0.04 sec) mysql>
mysql> insert into b values ('b',5),('c',15),('d',20),('e',99);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0 #将两张表合并,相同id的num相加
mysql> select id,sum(num) from (select * from a union all select * from b) as temp group by id;
+------+----------+
| id | sum(num) |
+------+----------+
| a | 5 |
| b | 15 |
| c | 30 |
| d | 30 |
| e | 99 |
+------+----------+
5 rows in set (0.05 sec)
#将两张表用union all合并,然后再按照id分组,求和,达到最终的目的
union的用法的更多相关文章
- union和union all用法
工作中,遇到同事之前写的oracle语句中有一个union all,并且很多地方都用到了.便在网上查了一下用法,以下是自己的理解. union (联合)将两个或者多个结果集合并. 在使用时,两个结果 ...
- Oracle 中union的用法
UNION 指令的目的是将两个 SQL 语句的结果合并起来,可以查看你要的查询结果. 例如: SELECT Date FROM Store_Information UNION SELECT Date ...
- 关于C与C++的struct,union,enum用法差异
对着代码说话: #include <stdio.h> #include <stdlib.h> struct test { int abc; }; enum _enum {A,B ...
- MySQL全连接(Full Join)实现,union和union all用法
MySQL本身不支持你所说的full join(全连接),但可以通过union来实现 ,下面是一个简单测试,可以看看: mysql> CREATE TABLE a(id int,name cha ...
- LInq之Take Skip TakeWhile SkipWhile Reverse Union Concat 用法
废话不多说,直接上代码,代码有注释!自行运行测试! class Program { static void Main(string[] args) { string[] names = { " ...
- MySQL的学习--join和union的用法
感觉工作之后一直在用框架,数据库的一些基本的东西都忘记了,这次借着这个系列的博客回顾一下旧知识,学一点新知识. 今天就先从join和union开始. join 是两张表做交连后里面条件相同的部分记录产 ...
- SQL Union和SQL Union All用法
SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每 ...
- sql语句查询结果合并union all用法
整理别人的sql 大概的思想是用union 和union all --合并重复行select * from Aunion select * from B --不合并重复行select * from A ...
- 一次mysql数据关于union+concat用法的记录
SELECT CONCAT('SELECT COUNT(*) FROM ',table_name,' union all') FROM information_schema.tables WHERE ...
随机推荐
- jquery封装的选项卡
ul,li,div{ margin:; padding:;} ul,li{ list-style:none;} .tab_wrap{ width:450px; margin: auto 50px; o ...
- Android与PHP服务器交互
转自:http://blog.csdn.net/ab_ba/article/details/7912424 服务器端:server.php 1 <?php 2 include(' ...
- POJ 1511 Invitation Cards dij
分析:正向加边,反向加边,然后两遍dij #include<cstdio> #include<cstring> #include<queue> #include&l ...
- Loadrunner 录制成功,但是脚本并没有产生
LR 在用IE 录制脚本已经成功,但是结束录制以后,并没有脚本产生,在产生脚本log 中提示: [Net An. Error (14c8:1cec)] Request Connection: R ...
- [CCC 1996 01]Deficient, Perfect, and Abundant
CCC加拿大高中生信息学奥赛 其余来源 CODEVS[3312]——CCC 1996 01 Deficient, Perfect, and Abundant ——http://codevs.cn/pr ...
- 使用Visual Studio 2013编写可维护的本地可视化(natvis)
在Visual Studio 2012中,我们介绍了创建可视化使用原生类型的能力natvis文件. Visual Studio 2013中包含了一些改进,使其更容易编写可视化的类,在内部利用收集来存储 ...
- Visual Studio的.NET内存分配分析器解析
Visual Studio 2012拥有丰富的有价值的功能,以至于我听到开发者反馈的需要的新功能新版本已经有了.另外,我听到开发人员询问具体的功能的某个特性,实际上他真正需要的是另外一个功能点. 上面 ...
- 【解决】Django下使用sqlite3的相关问题
最近在玩Django,想用它写一个很小很小的项目,Django自带数据库sqlite3,本来项目也小,我就用它了. 玩意虽小,东西却不是那么好用的. 首先,在项目中建立模型,一个例子是这样的: cla ...
- PHP写日志什么时候需要加锁?
先分析fwrite,直接找到PHP源代码: static size_t _php_stream_write_buffer(php_stream *stream, const char *buf, si ...
- Cassandra的登录认证授权
cassandra的登录验证机制是独自的,数据是集群共享的 参考:http://blog.csdn.net/y_y_y_k_k_k_k/article/category/5943357 1.初始安装启 ...