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

Examples of the usage of Union in SQL

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Editor to share with you an example of the use of Union in SQL. I hope you will gain something after reading this article. Let's discuss it together.

What is the use of sql union?

Summary of sql union usage:

The UNION operator combines the results of multiple SELECT statements into a single result set.

(1) the following conditions must be met for the use of UNION:

A: all queries must have the same structure (that is, the number and order of columns in the query must be the same).

B: the data types of the corresponding columns can be different but must be compatible (the so-called compatibility means that implicit conversions can be performed between the two types, and an error can be reported if there is no implicit conversion). You can also explicitly convert to the same data type.

(when the data type is different, the resulting data type is determined according to the data type priority rules. If the type is the same, but the precision, decimal places, or length are different, the result is determined according to the same rules used to merge expressions) (see MSDN: data type priority)

C: if it is a XML data type, the columns must be equivalent (all columns must be typed to XML schemas or untyped), and if typed, they must be typed to the same XML schema collection.

(2) UNION result set

The column name of the result set after A:UNION is the same as that of the result set of the first SELECT statement. The result set column name of another SELECT statement is ignored.

B:UNION deletes duplicate records in the result set by default, and keeps all records in the result set if you use the ALL keyword (keep duplicate records)

(3) pay attention.

Multiple UNION operators can be used in A: Transact-SQL statements

B: individual SELECT statements that use the UNION operator cannot contain their own ORDER BY or COMPUTE clauses. You can only use an ORDER BY or COMPUTE clause in the final combined result set (that is, after the last SELECT statement).

C: GROUP BY and HAVING clauses can be used in various SELECT statements that use the UNION operator.

D: by default, SQL Server 2005 evaluates statements that contain UNION operators from left to right. You can use parentheses to specify the evaluation order.

Examples of MSDN are as follows

For example, the following statement is not equivalent:

/ * First statement. * / SELECT * FROM TableA UNION ALL (SELECT * FROM TableB UNION SELECT * FROM TableC) GO / * Second statement. * / (SELECT * FROM TableA UNION ALL SELECT * FROM TableB) UNION SELECT * FROM TableC) GO has finished reading this article. I believe you have some understanding of "examples of the use of Union in SQL". If you want to know more about it, please follow the industry information channel. Thank you for reading!

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