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

MySQL 5.7.23 installation and configuration graphic tutorial

This article records the detailed installation pr...

How to convert Chinese into UTF-8 in HTML

In HTML, the Chinese phrase “學好好學” can be express...

Detailed explanation of three ways to wrap text in el-table header

Table of contents Problem Description Rendering T...

Detailed tutorial on deploying Django project using Docker on centos8

introduction In this article, we will introduce h...

KVM virtualization installation, deployment and management tutorial

Table of contents 1.kvm deployment 1.1 kvm instal...

HTML table border control implementation code

Generally, when we use a table, we always give it...

How to remove MySQL from Ubuntu and reinstall it

First delete mysql: sudo apt-get remove mysql-* T...

How to monitor and delete timed out sessions in Tomcat

Preface I accidentally discovered that the half-h...

Detailed explanation of querying JSON format fields in MySQL

During the work development process, a requiremen...

How to install and connect Navicat in MySQL 8.0.20 and what to pay attention to

Things to note 1. First, you need to create a my....

Using CSS3 to achieve transition and animation effects

Why should we use CSS animation to replace JS ani...

How to install MySQL database on Ubuntu

Ubuntu is a free and open source desktop PC opera...

HTTP Status Codes

This status code provides information about the s...