Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use union in mysql

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use union in mysql". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use union in mysql".

In mysql, union is used to combine the results of multiple select statements into a result set and remove duplicate data in the result set with the syntax "select column,...from table1 union select column,...from table2".

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

What is the use of union in mysql

1.mysql union syntax

Mysql union is used to combine results from multiple select statements into a single result set. The syntax is:

Select column,.from table1union [all] select column,. From table2...

In multiple select statements, the corresponding column should have the same field properties, and the field name used in the first select statement is also used for the field name of the result.

1.1 differences between union and union all

When using union, mysql deletes duplicate records in the result set, while with union all, mysql returns all records and is more efficient than union.

2. Example of mysql union usage

Union often queries two or more tables similar to data, such as different data classification tables, or data history tables. Here are two tables for testing.

2.1 use UNION query

Query the id number and title of the article in the two tables and remove the duplicate records:

SELECT aid,title FROM article UNION SELECT bid,title FROM blog

The returned query results are as follows:

2.2.UNION query result description

Duplicate records refer to records in which each field in the query completely duplicates. As in the above example, if the title is the same but the id number is not the same, it is regarded as a different record.

The field name used in the first SELECT statement is also used for the field name of the result, such as aid in the example above.

The field names of each SELECT statement can be different, but the field properties must be consistent.

3. Use UNION ALL query

Query the id number and title of the article in the two tables and return all records:

SELECT aid,title FROM article UNION ALL SELECT bid,title FROM blog

The returned query results are as follows:

Obviously, when using UNION ALL, you simply group the queries together without judging whether the data is duplicated. Therefore, UNION ALL should be used to improve query efficiency when it is determined that there will be no duplicate data in the query results or that there is no need to de-duplicate data.

Thank you for your reading, the above is the content of "how to use union in mysql". After the study of this article, I believe you have a deeper understanding of how to use union in mysql, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report