MySQL UNION OperatorThis tutorial introduces the syntax and examples of MySQL UNION operator. describeThe MySQL UNION operator is used to combine the results of two or more SELECT statements into one result set. Multiple SELECT statements will remove duplicate data. grammarMySQL UNION operator syntax format: SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions] UNION [ALL | DISTINCT] SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions]; parameter
Demo DatabaseIn this tutorial, we will use the RUNOOB sample database. Here is the data selected from the "Websites" table: mysql> SELECT * FROM Websites; +----+--------------+---------------------------+-------+---------+ | id | name | url | alexa | country | +----+--------------+---------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | | 2 | Taobao | https://www.taobao.com/ | 13 | CN | | 3 | Novice Tutorial | http://www.runoob.com/ | 4689 | CN | | 4 | Weibo | http://weibo.com/ | 20 | CN | | 5 | Facebook | https://www.facebook.com/ | 3 | USA | | 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND | +----+---------------+---------------------------+-------+---------+ Here are the data for the "apps" APP: mysql> SELECT * FROM apps; +----+------------+-------------------------+---------+ | id | app_name | url | country | +----+------------+-------------------------+---------+ | 1 | QQ APP | http://im.qq.com/ | CN | | 2 | Weibo APP | http://weibo.com/ | CN | | 3 | Taobao APP | https://www.taobao.com/ | CN | +----+------------+-------------------------+---------+ 3 rows in set (0.00 sec) SQL UNION ExampleThe following SQL statement selects all distinct countries (only distinct values) from the "Websites" and "apps" tables: ExamplesSELECT country FROM Websites UNION SELECT country FROM apps ORDER BY country; The output of executing the above SQL is as follows: ![]() Note: UNION cannot be used to list all countries in two tables. If some websites and apps are from the same country, each country will be listed only once. UNION will only select distinct values. Please use UNION ALL to select duplicate values! SQL UNION ALL ExampleThe following SQL statement uses UNION ALL to select all countries (also with duplicate values) from the "Websites" and "apps" tables: ExamplesSELECT country FROM Websites UNION ALL SELECT country FROM apps ORDER BY country; The output of executing the above SQL is as follows: ![]() SQL UNION ALL with WHEREThe following SQL statement uses UNION ALL to select all data for China (CN) from the "Websites" and "apps" tables (including duplicate values): ExamplesSELECT country, name FROM Websites WHERE country='CN' UNION ALL SELECT country, app_name FROM apps WHERE country='CN' ORDER BY country; The output of executing the above SQL is as follows: ![]() You may also be interested in:
|
<<: Solve the problem that Navicat cannot connect to MySQL on the Linux server
>>: Solutions to VMware workstation virtual machine compatibility issues
This article mainly introduces the solution to th...
Before using jQuery to complete the fade-in and f...
Because li is a block-level element and occupies ...
This article records the installation and configu...
1. How to create a user and password 1. Enter the...
today select * from table name where to_days(time...
This article describes how to install the PHP cur...
Table of contents 1. Database bottleneck 2. Sub-l...
Table of contents 1. MySQL data backup 1.1, mysql...
Hyperlinks enable people to jump instantly from pa...
Preface Since the types of the same fields in the...
It’s great to use CSS to realize various graphics...
Suggestion: Handwriting code as much as possible c...
Table of contents 1. Usage 2. Output results 1.id...
Basics A transaction is an atomic operation on a ...