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. PHP数组排列

    一.先看最简单的情况.有两个数组: $arr1 = array(1,9,5);$arr2 = array(6,2,4); array_multisort($arr1,$arr2); print_r($ ...

  2. POJ 2411 Mondriaan's Dream

    思路:状态压缩dp,如果在(i,j)位置横着放砖块,那么(i,j)和(i+1.j)都是1,如果竖着放砖块,那么(i,j)为0,(i,j+1)为1,这样每行就可以用一个整数来存放状态,设dp[i][j] ...

  3. SQL SERVER 作业(或叫执行计划)

    如果在SQL Server 里需要定时或者每隔一段时间执行某个存储过程或3200字符以内的SQL语句时,可以用管理->SQL Server代理->作业来实现. 1.管理->SQL S ...

  4. 提升WI-FI信号强度的10大方法

    原文链接:http://server.51cto.com/Net-402889.htm

  5. 2015年9月28日JQuery提前预习预热笔记

    visual studio下载2010 2010与2008不一样,2008需要添加补丁,采用调用对象.2010可以直接用. JQuery=$ 是函数是方法是对象 念J快儿,念doler 开发人员工具( ...

  6. 【CSS3】Advanced10:Gradient

    1.background:linear-gradient(20deg/(to) bottom right,orange,red,hsl(60,100%,50%)); 2.-webkit-chrome/ ...

  7. 【OpenCV学习笔记】之六 手写图像旋转函数---万丈高楼平地起

    话说,平凡之处显真格,这一点也没错!  比如,对旋转图像进行双线性插值,很简单吧?  可,对我,折腾了大半天,也没有达到预期效果!  尤其是三个误区让我抓瞎好久: 1,坐标旋转公式.   这东西,要用 ...

  8. 【python自动化第十一篇】

    [python自动化第十一篇:] 课程简介 gevent协程 select/poll/epoll/异步IO/事件驱动 RabbitMQ队列 上节课回顾 进程: 进程的诞生时为了处理多任务,资源的隔离, ...

  9. HDU P4578 Transformation

    Problem Description Yuanfang is puzzled with the question below: There are n integers, a1, a2, …, an ...

  10. jsp页面转发到servlet

    一个简单的例子来了解一下jsp页面转发到servlet的过程,环境 eclipse.tomcat 1.工程目录结构如下 2.各部分代码如下 1>index.jsp <%@ page lan ...