Home > Database > Mysql Tutorial > MySQL query data merge query results

MySQL query data merge query results

coldplay.xixi
Release: 2021-03-17 09:32:55
forward
4749 people have browsed it

MySQL query data merge query results.

  • Using the union keyword, you can give multiple select statements and combine their results into a single result set. When merging, the number of columns and data types corresponding to the two tables must be the same. Use the union or union all keyword to separate each select statement.
  • Union does not use the keyword all. Duplicate records are deleted during execution, and all returned rows are unique. The function of using the keyword all is not to delete duplicate rows and not automatically sort the results.
    The basic syntax format is:
select column,...from table1union [all]select column,... from table2
Copy after login

(free learning recommendation: mysql video tutorial)

[Example 1] Query the information of all fruits whose price is less than 9, query the information of all fruits with s_id equal to 101 and 103, use union to connect the query results, the SQL statement is as follows:

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)
Copy after login

Union combines the results of multiple select statements into a result set. You can view the results of each select statement separately:

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)
Copy after login

As you can see from the separate query results, the first select statement queries fruits with a price less than 9, and the second select statement queries the fruits provided by suppliers 101 and 103. fruit.

  • Use union to separate two select statements. After execution, combine the output results into a single result set and delete duplicate records.
  • Use union all to include duplicate rows. Union automatically removes duplicate rows from the query result set. If you want to return all matching rows without deleting, you can use union all.

[Example 2] Query the information of all fruits with a price less than 9, query the information of all fruits with s_id equal to 101 and 103, use union all to connect the query results, the SQL statement is as follows:

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)
Copy after login

As you can see, the total number of records here is equal to the sum of the number of records returned by the two select statements. The connection query results do not remove duplicate rows.

The difference between union and union all:

  • The function of using union all is not to delete duplicate rows, and the all keyword statement is executed It requires few resources, so use it as much as possible.
  • When you are sure that there will be no duplicate data in the query results or there is no need to remove duplicate data, you should try to use uninon all to improve query efficiency.

More related free learning recommendations: mysql tutorial(Video)

The above is the detailed content of MySQL query data merge query results. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template