MySQL UNION operator basic knowledge points

MySQL UNION operator basic knowledge points

MySQL UNION Operator

This tutorial introduces the syntax and examples of MySQL UNION operator.

describe

The 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.

grammar

MySQL 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

  • expression1, expression2, ... expression_n : The columns to be retrieved.

  • tables: The data tables to be retrieved.

  • WHERE conditions: Optional, search conditions.

  • DISTINCT: Optional, remove duplicate data in the result set. By default, the UNION operator removes duplicate data, so the DISTINCT modifier has no effect on the result.

  • ALL: Optional, returns all result sets, including duplicate data.

Demo Database

In 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 Example

The following SQL statement selects all distinct countries (only distinct values) from the "Websites" and "apps" tables:

Examples

SELECT 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 Example

The following SQL statement uses UNION ALL to select all countries (also with duplicate values) from the "Websites" and "apps" tables:

Examples

SELECT 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 WHERE

The following SQL statement uses UNION ALL to select all data for China (CN) from the "Websites" and "apps" tables (including duplicate values):

Examples

SELECT 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:
  • Introduction to the use of MySQL joint query UNION and UNION ALL
  • How to use union and order by at the same time in MySQL
  • Detailed explanation of the usage of UNION in MySQL
  • Comparison of the efficiency of using or, in and union all in MySQL query commands
  • Solution to the error problem when using UNION and Order by in Mysql joint query
  • Tutorial on the difference between union and join statements in MySQL
  • MySQL union syntax code example analysis
  • Brief analysis of MySQL union and union all
  • Study on the default rules of mySQL UNION operator
  • Detailed explanation of the failure of MySQL to use UNION to connect two queries

<<:  Solve the problem that Navicat cannot connect to MySQL on the Linux server

>>:  Solutions to VMware workstation virtual machine compatibility issues

Recommend

Solution to the automatic stop of MySQL service

This article mainly introduces the solution to th...

jQuery achieves fade-in and fade-out effects

Before using jQuery to complete the fade-in and f...

Two ways to achieve horizontal arrangement of ul and li using CSS

Because li is a block-level element and occupies ...

MySQL 8.0.15 installation and configuration graphic tutorial under Win10

This article records the installation and configu...

How to create users and manage permissions in MySQL

1. How to create a user and password 1. Enter the...

mysql query data for today, this week, this month, and last month

today select * from table name where to_days(time...

Detailed explanation of how to install PHP curl extension under Linux

This article describes how to install the PHP cur...

Summary of MySQL's commonly used database and table sharding solutions

Table of contents 1. Database bottleneck 2. Sub-l...

MySQL scheduled full database backup

Table of contents 1. MySQL data backup 1.1, mysql...

Improving the effect of hyperlinks in web design and production

Hyperlinks enable people to jump instantly from pa...

MYSQL string forced conversion method example

Preface Since the types of the same fields in the...

CSS3 realizes various graphic effects of small arrows

It’s great to use CSS to realize various graphics...

How to customize more beautiful link prompt effect with CSS

Suggestion: Handwriting code as much as possible c...

MySQL EXPLAIN statement usage examples

Table of contents 1. Usage 2. Output results 1.id...

How MySQL uses transactions

Basics A transaction is an atomic operation on a ...