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:
Query two:
Union all of the two result sets (directly merge the two query result sets):
Union of the two result sets, the result is (two After merging the result sets, perform deduplication/distinct):
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!