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

The usage of sql Union and Union All

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article mainly explains "the usage of sql Union and Union All". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the usage of sql Union and Union All.

One limitation of UNION is that the fields generated by two SQL statements need to be of the same type of data. In addition, when we use the UNION instruction, we only see different data values (similar to SELECT DISTINCT). Union only joins two results together to display, not join two tables

The syntax of UNION is as follows: [SQL statement 1]

UNION

[SQL statement 2] suppose we have the following two tables

Store_Information form store_name Sales Date

Los Angeles $1500 Jan-05-1999

San Diego $250 Jan-07-1999

Los Angeles $300 Jan-08-1999

Boston $700 Jan-08-1999

Internet Sales form Date Sales

Jan-07-1999 $250

Jan-10-1999 $535

Jan-11-1999 $320

Jan-12-1999 $750

And we need to find out all the days when there is a sales. To do this, we use the following SQL statement:

SELECT Date FROM Store_Information

UNION

SELECT Date FROM Internet_Sales results:

Date

Jan-05-1999

Jan-07-1999

Jan-08-1999

Jan-10-1999

Jan-11-1999

Jan-12-1999

It is worth noting that if we use "SELECT DISTINCT Date" in any SQL statement (or both), we will get exactly the same result.

SQL Union All

The purpose of the UNION ALL directive is also to merge the results of the two SQL statements. UNION ALL differs from UNION in that UNION ALL lists every piece of data that meets the criteria, regardless of whether the data value is duplicated or not. The syntax of UNION ALL is as follows: [SQL statement 1]

UNION ALL

[SQL statement 2] We use the same example as the previous page to show the difference between UNION ALL and UNION. Again, suppose we have the following two tables:

Store_Information form store_name Sales Date

Los Angeles $1500 Jan-05-1999

San Diego $250 Jan-07-1999

Los Angeles $300 Jan-08-1999

Boston $700 Jan-08-1999

Internet Sales form Date Sales

Jan-07-1999 $250

Jan-10-1999 $535

Jan-11-1999 $320

Jan-12-1999 $750

And we need to find out the days when there are store turnover and online turnover. To do this, we use the following SQL statement:

SELECT Date FROM Store_Information

UNION ALL

SELECT Date FROM Internet_Sales results:

Date

Jan-05-1999

Jan-07-1999

Jan-08-1999

Jan-08-1999

Jan-07-1999

Jan-10-1999

Jan-11-1999

Jan-12-1999

At this point, I believe you have a deeper understanding of the use of "sql Union and Union All". 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.

Share To

Database

Wechat

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

12
Report