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

Usage of union and union all in mysql

王林
王林forward
2020-06-12 15:56:194225browse

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete