Home > Database > Mysql Tutorial > Usage of union and union all in mysql

Usage of union and union all in mysql

王林
Release: 2020-06-12 15:56:19
forward
4224 people have browsed it

Usage of union and union all in mysql

When to use union and union all?

If after we query the data of the two tables according to certain query conditions, we need to merge the results together and display them, then at this time we need to use union and union all.

The main difference between union and union all:

union all directly merges the result sets together;

union is after union all The result is distinct, and the result after removing duplicate records is output.

Let’s look at the following example:

Query one:

Usage of union and union all in mysql

Query two:

Usage of union and union all in mysql

Union all of the two result sets (directly merge the two query result sets):

Usage of union and union all in mysql

Union of the two result sets, the result is (two After merging the result sets, perform deduplication/distinct):

Usage of union and union all in mysql

Recommended tutorial: mysql tutorial

The above is the detailed content of Usage of union and union all in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template