MySQL查询数据之合并查询结果


本文摘自PHP中文网,作者coldplay.xixi,侵删。

.

  • 利用union关键字,可以给出多条select语句,并将它们的结果组合成单个结果集。合并时,两个表对应的列数和数据类型必须相同。各个select语句之间使用union或union all 关键字分隔。
  • union不使用关键字all,执行的时候删除重复的记录,所有返回的行都是唯一的;使用关键字all的作用是不删除重复行也不对结果进行自动排序。
    基本语法格式为:

1

select column,...from table1union [all]select column,... from table2

(免费学习推荐:mysql视频教程

【例1】查询所有价格小于9的水果的信息,查询s_id等于101和103所有的水果的信息,使用union连接查询结果,SQL语句如下:

1

2

3

mysql> select s_id,f_name,f_price    -> from fruits    -> where f_price <9.0

    -> union all

    -> select s_id,f_name,f_price    -> from fruits    -> where s_id in(101,103);+------+------------+---------+| s_id | f_name     | f_price |+------+------------+---------+|  104 | lemon      |    6.40 ||  101 | apple      |    5.20 ||  103 | apricot    |    2.20 ||  104 | berry      |    7.60 ||  107 | xxxx       |    3.60 ||  105 | melon      |    8.20 ||  101 | cherry     |    3.20 ||  105 | xbabay     |    2.60 ||  102 | grape      |    5.30 ||  107 | xbabay     |    3.60 ||  101 | apple      |    5.20 ||  103 | apricot    |    2.20 ||  101 | blackberry |   10.20 ||  101 | cherry     |    3.20 ||  103 | coconut    |    9.20 |+------+------------+---------+15 rows in set (0.06 sec)

union将多个select语句的结果组合成一个结果集合。可以分开查看每个select语句的结果:

1

mysql> select s_id,f_name,f_price    -> from fruits    -> where f_price < 9.0;+------+---------+---------+| s_id | f_name  | f_price |+------+---------+---------+|  104 | lemon   |    6.40 ||  101 | apple   |    5.20 ||  103 | apricot |    2.20 ||  104 | berry   |    7.60 ||  107 | xxxx    |    3.60 ||  105 | melon   |    8.20 ||  101 | cherry  |    3.20 ||  105 | xbabay  |    2.60 ||  102 | grape   |    5.30 ||  107 | xbabay  |    3.60 |+------+---------+---------+10 rows in set (0.00 sec)mysql> select s_id,f_name,f_price    -> from fruits    -> where s_id in(101,103);+------+------------+---------+| s_id | f_name     | f_price |+------+------------+---------+|  101 | apple      |    5.20 ||  103 | apricot    |    2.20 ||  101 | blackberry |   10.20 ||  101 | cherry     |    3.20 ||  103 | coconut    |    9.20 |+------+------------+---------+5 rows in set (0.00 sec)

由分开查询结果可以看到,第1条select语句查询价格小于9的水果,第2条select语句查询供应商101和103提供的水果。

  • 使用union将两条select语句分隔开,执行完毕之后把输出结果组合成单个的结果集,并删除重复的记录。
  • 使用union all包含重复的行。union从查询结果集中自动去除了重复的行,如果要返回所有匹配的行,而不进行删除,可以用union all。

【例2】查询所有价格小于9的水果的信息,查询s_id等于101和103的所有水果的信息,使用union all连接查询结果,SQL语句如下:

1

2

3

mysql> select s_id,f_name,f_price    -> from fruits    -> where f_price<9.0

    -> union all

    -> select s_id,f_name,f_price    -> from fruits    -> where s_id in(101,103);+------+------------+---------+| s_id | f_name     | f_price |+------+------------+---------+|  104 | lemon      |    6.40 ||  101 | apple      |    5.20 ||  103 | apricot    |    2.20 ||  104 | berry      |    7.60 ||  107 | xxxx       |    3.60 ||  105 | melon      |    8.20 ||  101 | cherry     |    3.20 ||  105 | xbabay     |    2.60 ||  102 | grape      |    5.30 ||  107 | xbabay     |    3.60 ||  101 | apple      |    5.20 ||  103 | apricot    |    2.20 ||  101 | blackberry |   10.20 ||  101 | cherry     |    3.20 ||  103 | coconut    |    9.20 |+------+------------+---------+15 rows in set (0.00 sec)

可以看到,这里总的记录等于两条select语句返回的记录数之和,连接查询结果并没有去除重复的行。

union和union all的区别:

  • 使用union all的功能是不删除重复行,all关键字语句执行时所需要的资源少, 所以尽可能的使用它。
  • 确定查询结果中不会有重复数据或者不需要去掉重复数据的时候,应当尽量使用uninon all以提高查询效率。

更多相关免费学习推荐:mysql教程(视频)

以上就是MySQL查询数据之合并查询结果的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

win10下mysql 8.0.12 安装及环境变量配置教程

mysql模糊查询语句是什么?

mysql中优化和修复数据库工具mysqlcheck详细介绍

mysql常用sql语句总结包含复杂sql查询

mysql中插入表数据中文乱码怎么办

浅谈mysql原理与优化(一)之历史与体系结构

mysql如何查询表中某行数据

如何源码安装mysql数据库?

mysql实现字符串截取的图文教程

布尔教育燕十八mysql优化视频课件源码分享

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...