Get the App
SLTechnology News&Howtos  ›  Database  › 

The difference between union and union all in mysql Database

Shulou Source: shulou.com Published: 2022-06-01 16:47:32 10月03日 Update

This article uses easy-to-understand examples to introduce the difference between union and union all in mysql database. The content of the article is compact step by step. I hope you can get something according to this article.

When do I use union and union all?

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

The main differences between union and union all:

Union all is to merge result sets directly together.

Union is a distinct of the results after union all, and the output results after removing the duplicate records.

Let's look at the following example:

Enquiry 1:

Enquiry 2:

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

The two result sets are union, and the result is (deduplicated / distinct after the two result sets are merged):

After reading the above, do you have a general idea of the difference between union and union all in the mysql database? If you want to know more about the content of the article, welcome to follow the industry information channel, thank you for reading!

Tags: Results two queries data content databases examples articles articles compact easy to understand more conditions industry information information channels face channels output Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi macOS MySQL Microsoft