SQL Server Union and SQL UNION all use methods _mssql

Source: Internet
Author: User
Tags sql server union

SQL UNION operator

The UNION operator is used to merge the result sets of two or more SELECT statements.

Note that the SELECT statement within the UNION must have the same number of columns. The column must also have a similar data type . Also, the order of the columns in each SELECT statement must be the same.

SQL UNION Syntax
SELECT column_name (s) from table_name1
UNION
SELECT column_name (s) from table_name2

Note: By default, the union operator chooses a different value, that is, the union is gone heavy . If duplicate values are allowed, use UNIONall.

SQL UNION All syntax
SELECT column_name (s) from table_name1
UNION All
SELECT column_name (s) from table_name2

In addition, the column names in the union result set are always equal to the column names in the first SELECT statement in the Union.

The purpose of the UNION directive is to combine the results of two SQL statements. From this point of view, the UNION is somewhat similar to the JOIN, since these two instructions can be retrieved from multiple tables. The union simply joins the two results together to show that it is not connected to two tables ... The UNION 's syntax is as follows:

[SQL statement 1]
UNION
[SQL Statement 2]
Let's say we have the following two tables,

store_information Table
Store_name Sales Date
Los Angeles $1500 jan-05-1999
San Diego $ jan-07-1999
Los Angeles $300 jan-08-1999
Boston $700 jan-08-1999
Internet Sales table
Date Sales
jan-07-1999 $
jan-10-1999 $535
jan-11-1999 $320
jan-12-1999 $750

And we're going to find all the sales days. To achieve 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 of the SQL statements (or both), we will get exactly the same result.

SQL Union All
UNION All the purpose of this instruction is to merge the results of two SQL statements together. The union All and the union differ in that the union all lists each qualifying data, regardless of whether the data value is duplicated or not. The syntax for 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 . Also assume that we have the following two forms,

store_information Table
Store_name Sales Date
Los Angeles $1500 jan-05-1999
San Diego $ jan-07-1999
Los Angeles $300 jan-08-1999
Boston $700 jan-08-1999
Internet Sales table
Date Sales
jan-07-1999 $
jan-10-1999 $535
jan-11-1999 $320
jan-12-1999 $750

And we need to find out where the store turnover and network turnover date. To achieve 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

eg

Copy Code code as follows:

SELECT Id,name,docpinyin from Doctor
UNION
SELECT 0 as ID, ' No doctor name ' as name, ' as Docpinyin


The result set is

SELECT Id,name,docpinyin from Doctor

The result set is

So: Union sets two results together

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.