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 ...
随机推荐
- [NYOJ 536] 开心的mdd
开心的mdd 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述himdd有一天闲着无聊,随手拿了一本书,随手翻到一页,上面描述了一个神奇的问题,貌似是一个和矩阵有关的东西. ...
- 用jQuery 处理XML-- jQuery与XML
jQuery与XML 快而强的遍历系统,华丽丽的选择器语法,这或许是jQuery 那么流行的原因.当然它还有详尽的文档.它主要是用来处理HTML的,但在这里妳会看到如何应用到XML. 使用jQuery ...
- windows查看服务端口
开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...
- wpa_supplicant 与iwpriv工具配置WIFI的命令
=====================================================hostapd 配置命令=================================== ...
- Selenium 实现联想下拉框
直接在文本框输入字符,并不能实现联想下拉框, 第一种方式:强制执行js driver.FindElement(By.Id("top_search_input")).SendKeys ...
- JZ2440开发笔记(3)——配置TFTP
第一步: 1.关闭ubuntu的防火墙 ufw disable2.卸载了iptables apt-get remove iptables 1.用iptables -F这个命令来关闭防火 ...
- mac os develop
. 安装PCRE Download latest PCRE. After download go to download directory from terminal. $ cd ~/Downloa ...
- HW4.46
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- WPF布局系统[转]
转自:http://www.cnblogs.com/niyw/archive/2010/10/31/1863908.html前言 前段时间忙了一阵子Google Earth,这周又忙了一阵子架构师论文 ...
- 跟我一起写Makefile:MakeFile介绍
makefile 介绍 make命令执行时,需要一个 makefile 文件,以告诉make命令如何去编译和链接程序. 首先,我们用一个示例来说明makefile的书写规则.以便给大家一个感性认识.这 ...