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的用法的更多相关文章

  1. union和union all用法

    工作中,遇到同事之前写的oracle语句中有一个union all,并且很多地方都用到了.便在网上查了一下用法,以下是自己的理解. union  (联合)将两个或者多个结果集合并. 在使用时,两个结果 ...

  2. Oracle 中union的用法

    UNION 指令的目的是将两个 SQL 语句的结果合并起来,可以查看你要的查询结果. 例如: SELECT Date FROM Store_Information UNION SELECT Date ...

  3. 关于C与C++的struct,union,enum用法差异

    对着代码说话: #include <stdio.h> #include <stdlib.h> struct test { int abc; }; enum _enum {A,B ...

  4. MySQL全连接(Full Join)实现,union和union all用法

    MySQL本身不支持你所说的full join(全连接),但可以通过union来实现 ,下面是一个简单测试,可以看看: mysql> CREATE TABLE a(id int,name cha ...

  5. LInq之Take Skip TakeWhile SkipWhile Reverse Union Concat 用法

    废话不多说,直接上代码,代码有注释!自行运行测试! class Program { static void Main(string[] args) { string[] names = { " ...

  6. MySQL的学习--join和union的用法

    感觉工作之后一直在用框架,数据库的一些基本的东西都忘记了,这次借着这个系列的博客回顾一下旧知识,学一点新知识. 今天就先从join和union开始. join 是两张表做交连后里面条件相同的部分记录产 ...

  7. SQL Union和SQL Union All用法

    SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每 ...

  8. sql语句查询结果合并union all用法

    整理别人的sql 大概的思想是用union 和union all --合并重复行select * from Aunion select * from B --不合并重复行select * from A ...

  9. 一次mysql数据关于union+concat用法的记录

    SELECT CONCAT('SELECT COUNT(*) FROM ',table_name,' union all') FROM information_schema.tables WHERE ...

随机推荐

  1. [POJ 1151] Atlantis

    一样的题:HDU 1542 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18148   Accepted ...

  2. Bootstrap中的less基础

    在线编译 因为 less 的语法毕竟相对简单,所以一些在线工具可以很轻松的做到.比如 http://less.cnodejs.net http://www.ostools.net/less  一般都有 ...

  3. jquery插件cloud-zoom(放大镜)

    效果预览:http://www.helloweba.com/demo/cloud-zoom/ 源代码下载:http://pan.baidu.com/s/1eQnadXo Cloud Zoom是一个图像 ...

  4. (转载)file_get_contents("php://input")

    (转载)http://taoshi.blog.51cto.com/1724747/1165499 $data = file_get_contents("php://input"); ...

  5. Java数值转化为二进制、十进制、十六进制字符串

    int i = 123;String binStr = Integer.toBinaryString(i); String otcStr = Integer.toOctalString(i); Str ...

  6. [Raobin] Ext.net在多重子父窗体中找到当前窗体的父窗体,并关闭IFrame父窗体

    var closeParentWindow = function () { var currentWin = window; while (top != currentWin) { var prent ...

  7. seleniumRC启动及浏览器实例配置

    一.firefox浏览器实例配置 1.启动用户配置文件管理器 重要:在启动用户配置文件管理器之前,Firefox必须完全关闭.     1)按 support.cdn.mozilla.net/medi ...

  8. Android ListView 全面优化

    结合昨天学习的多线程,今天又继续对ListView进行了优化,包括异步加载图片,滑动时暂停加载,滑动停止后再加载显示界面中的item. 综合ListView在使用时参考的多篇博客,这里对ListVie ...

  9. 【原创】Mac上编译Hadoop1.0.3出现的一些问题

    create-native-configure: [exec] configure.ac:47: error: possibly undefined macro: AC_PROG_LIBTOOL [e ...

  10. PC-改变电脑的CPU,内存,硬盘大小!

    如何修改CPU频率及内存容量和硬盘大小 改变电脑的CPU,内存,硬盘大小!--------------------------------------------------------------- ...