In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 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 all in mysql". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use union all in mysql.
In mysql, the use of union all is "select statement 1 union all select statement 2." union all can combine the results from multiple select statements into a result set and return all records, which is more efficient than union.
The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.
What is the use of union all 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.
At this point, I believe you have a deeper understanding of "how to use union all in mysql". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
1.redis batch operation cat insert_redis.txt | redis-cli-h 10.24.253.1-p 7389-- pipe
© 2024 shulou.com SLNews company. All rights reserved.