MySQL数据库查询数据之合并查询结果 MySQL数据库使用教程

凤凰 2021-3-18 95 3/18

MySQL数据库查询数据之合并查询结果 MySQL数据库使用教程.

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

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

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

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语句的结果:

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语句如下:

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查询数据之合并查询结果的详细内容,更多请关注本站其它相关文章!

- THE END -

凤凰

3月18日09:09

最后修改:2021年3月18日
0

非特殊说明,本博所有文章均为博主原创。