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 records the detailed installation pr...
In HTML, the Chinese phrase “學好好學” can be express...
Table of contents Problem Description Rendering T...
introduction In this article, we will introduce h...
Table of contents 1.kvm deployment 1.1 kvm instal...
Generally, when we use a table, we always give it...
First delete mysql: sudo apt-get remove mysql-* T...
Preface I accidentally discovered that the half-h...
During the work development process, a requiremen...
Things to note 1. First, you need to create a my....
Why should we use CSS animation to replace JS ani...
Ubuntu is a free and open source desktop PC opera...
From the backend to the front end, what a tragedy....
Table of contents question Solution question Ther...
This status code provides information about the s...