Home Database Mysql Tutorial sqlserver Union和SQL Union All使用方法

sqlserver Union和SQL Union All使用方法

Jun 07, 2016 pm 06:01 PM
union

UNION 操作符用于合并两个或多个 SELECT 语句的结果集。

SQL UNION 操作符

UNION 操作符用于合并两个或多个 SELECT 语句的结果集。

请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。

SQL UNION 语法
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2

注释:默认地,UNION 操作符选取不同的值,即UNION是去了重的。如果允许重复的值,请使用 UNION ALL。

SQL UNION ALL 语法
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2

另外,UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。

UNION 指令的目的是将两个 SQL 语句的结果合并起来。从这个角度来看, UNION 跟 JOIN 有些许类似,因为这两个指令都可以由多个表格中撷取资料。union只是将两个结果联结起来一起显示,并不是联结两个表………… UNION 的语法如下:

[SQL 语句 1]
UNION
[SQL 语句 2]
假设我们有以下的两个表格,

Internet Sales 表格

而我们要找出来所有有营业额 (sales) 的日子。要达到这个目的,我们用以下的 SQL 语句: SELECT Date FROM Store_Information
UNION
SELECT Date FROM Internet_Sales
结果:

有一点值得注意的是,如果我们在任何一个 SQL 语句 (或是两句都一起) 用 "SELECT DISTINCT Date" 的话,那我们会得到完全一样的结果。

SQL Union All
UNION ALL 这个指令的目的也是要将两个 SQL 语句的结果合并在一起。 UNION ALLUNION 不同之处在于 UNION ALL 会将每一笔符合条件的资料都列出来,无论资料值有无重复。 UNION ALL 的语法如下: [SQL 语句 1]
UNION ALL
[SQL 语句 2]
我们用和上一页同样的例子来显示出 UNION ALLUNION 的不同。同样假设我们有以下两个表格,

Internet Sales 表格

而我们要找出有店面营业额以及网络营业额的日子。要达到这个目的,我们用以下的 SQL 语句: SELECT Date FROM Store_Information
UNION ALL
SELECT Date FROM Internet_Sales
结果:

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use union in c language How to use union in c language Sep 27, 2023 am 11:00 AM

The use of union in C language is a special data type that allows different data types to be stored in the same memory location. The use of union can help us save memory space and facilitate conversion between different data types. When using union, you need to note that the corresponding member is valid and only one member can be accessed at the same time.

How to use the FULL OUTER JOIN function in MySQL to obtain the union of two tables How to use the FULL OUTER JOIN function in MySQL to obtain the union of two tables Jul 26, 2023 pm 05:45 PM

How to use the FULLOUTERJOIN function in MySQL to obtain the union of two tables. In MySQL, the FULLOUTERJOIN function is a powerful join operation that combines inner joins and outer joins. It can be used to get the union of two tables, that is, combine all the data in the two tables into a single result set. This article will introduce the usage of the FULLOUTERJOIN function and provide some sample code to help readers better understand. FULLOUTERJOIN function

How to use mysql union to implement full outer join query How to use mysql union to implement full outer join query May 30, 2023 pm 06:49 PM

1. Union is not a method of multi-table connection query. It combines the query results of multiple query sentences into one result and removes duplicate data. 2. Full outer join queries the data of the left table and the right table, and then connects according to the connection conditions. Example #Use the left outer Aunion and the right outer BSELECT*FROMt_categorycLEFTOUTERJOINt_productpONc.cid=p.cnounionSELECT*FROMt_categorycRIGHTOUTERJOINt_productpONc.cid=p.cno

How does java define the Union class to realize the coexistence of data bodies? How does java define the Union class to realize the coexistence of data bodies? May 14, 2023 pm 03:34 PM

Define the Union class to implement the coexistence of data bodies. In the C/C++ language, a union, also known as a union, is a data structure similar to a struct. A union, like a struct, can contain many data types and variables. The difference between the two is as follows: all variables in a struct "coexist", and all variables are effective at the same time. Each variable occupies Different memory spaces; in a union, each variable is "mutually exclusive", only one variable is effective at the same time, and all variables occupy the same memory space. When multiple data need to share memory or only one of multiple data needs to be taken at a time, a union can be used. in Java

How to use Union to optimize Like statement in MySQL How to use Union to optimize Like statement in MySQL May 31, 2023 pm 03:55 PM

Optimize the Like statement with Union 1) Sometimes, you may need to use the or operator for comparison in the query. When the or keyword is used too frequently in the where clause, it may cause the MySQL optimizer to mistakenly choose a full table scan to retrieve records. The union clause can make queries execute faster, especially when one of the queries has an optimized index and the other query also has an optimized index. For example, when there are indexes on first_name and last_name respectively, execute the following query statement: mysql>select*fromstudentswherefirst_namelike'A

How to optimize UNION through MySQL to improve performance How to optimize UNION through MySQL to improve performance May 11, 2023 pm 05:40 PM

In many database applications, we are faced with situations where we need to integrate data from multiple data sources. MySQL's UNION statement is a way to solve this situation, which allows us to merge the result sets of two or more SELECT statements into one. While this is a very convenient feature, UNION statements can also cause performance issues on your system if not optimized. This article will explore how to optimize UNION to improve performance through MySQL. Use UNIONALL while using U

What are the points to note when using union in mysql? What are the points to note when using union in mysql? Jun 03, 2023 pm 08:04 PM

1. The union operator is used to combine the results of two or more select statements into a result set. Multiple select statements will delete duplicate data. 2. When using union to merge result sets, the number of columns in the two result sets is required to be the same. Exampleselectplayerno,townfromPLAYERSwheretown='Inglewood' unionselectplayerno,townfromPLAYERSwheretown='Plymouth';

What is the difference between union and unionall in MySQL What is the difference between union and unionall in MySQL May 30, 2023 am 08:04 AM

union: Perform a union operation on multiple result sets, excluding duplicate rows, and sort them at the same time. unionall: Performs a union operation on multiple result sets, including duplicate rows, without sorting. Query the information of employees whose department is less than 30, and the information of employees whose department is greater than 20 and less than 40. ①. First query the information of employees whose department number is less than 30. SELECTemployees_id,last_name,salary,department_idFROMemployeesWHEREdepartment_id

See all articles