.
select column,...from table1union [all]select column,... from table2
(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)
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)
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.
[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)
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:
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. 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!