UNION 指令的目的是将两个 SQL 语句的结果合并起来。从这个角度来看, UNION 跟 JOIN 有些许类似,因为这两个指令都可以由多个表格中撷取资料。 UNION 的一个限制是两个 SQL 语句所产生的栏位需要是同样的资料种类。另外,当我们用 UNION这个指令时,我们只会看到不同的资料值 (类似 SELECT DISTINCT)。 union只是将两个结果联结起来一起显示,并不是联结两个表………… UNION 的语法如下: [SQL 语句 1] UNION [SQL 语句 2] 假设我们有以下的两个表格,[table][tr][td]Store_Information 表格[table][tr][td]store_name [/td][td]Sales [/td][td]Date [/td][/tr][tr][td]Los Angeles [/td][td]$1500 [/td][td]Jan-05-1999 [/td][/tr][tr][td]San Diego [/td][td]$250 [/td][td]Jan-07-1999 [/td][/tr][tr][td]Los Angeles [/td][td]$300 [/td][td]Jan-08-1999 [/td][/tr][tr][td]Boston [/td][td]$700 [/td][td]Jan-08-1999 [/td][/tr][/table][/td][/tr][tr][td]Internet Sales 表格[table][tr][td]Date [/td][td]Sales [/td][/tr][tr][td]Jan-07-1999 [/td][td]$250 [/td][/tr][tr][td]Jan-10-1999 [/td][td]$535 [/td][/tr][tr][td]Jan-11-1999 [/td][td]$320 [/td][/tr][tr][td]Jan-12-1999 [/td][td]$750 [/td][/tr][/table][/td][/tr][/table]而我们要找出来所有有营业额 (sales) 的日子。要达到这个目的,我们用以下的 SQL 语句: SELECT Date FROM Store_Information UNION SELECT Date FROM Internet_Sales结果:[table][tr][td]Date[/td][/tr][tr][td]Jan-05-1999[/td][/tr][tr][td]Jan-07-1999[/td][/tr][tr][td]Jan-08-1999[/td][/tr][tr][td]Jan-10-1999[/td][/tr][tr][td]Jan-11-1999[/td][/tr][tr][td]Jan-12-1999[/td][/tr][/table]有一点值得注意的是,如果我们在任何一个 SQL 语句 (或是两句都一起) 用 "SELECT DISTINCT Date" 的话,那我们会得到完全一样的结果。
SQL Union All UNION ALL 这个指令的目的也是要将两个 SQL 语句的结果合并在一起。 UNION ALL 和 UNION 不同之处在于 UNION ALL 会将每一笔符合条件的资料都列出来,无论资料值有无重复。 UNION ALL 的语法如下: [SQL 语句 1] UNION ALL [SQL 语句 2] 我们用和上一页同样的例子来显示出 UNION ALL 和 UNION 的不同。同样假设我们有以下两个表格,[table][tr][td]Store_Information 表格[table][tr][td]store_name [/td][td]Sales [/td][td]Date [/td][/tr][tr][td]Los Angeles [/td][td]$1500 [/td][td]Jan-05-1999 [/td][/tr][tr][td]San Diego [/td][td]$250 [/td][td]Jan-07-1999 [/td][/tr][tr][td]Los Angeles [/td][td]$300 [/td][td]Jan-08-1999 [/td][/tr][tr][td]Boston [/td][td]$700 [/td][td]Jan-08-1999 [/td][/tr][/table][/td][/tr][tr][td]Internet Sales 表格[table][tr][td]Date [/td][td]Sales [/td][/tr][tr][td]Jan-07-1999 [/td][td]$250 [/td][/tr][tr][td]Jan-10-1999 [/td][td]$535 [/td][/tr][tr][td]Jan-11-1999 [/td][td]$320 [/td][/tr][tr][td]Jan-12-1999 [/td][td]$750 [/td][/tr][/table][/td][/tr][/table]而我们要找出有店面营业额以及网络营业额的日子。要达到这个目的,我们用以下的 SQL 语句: SELECT Date FROM Store_Information UNION ALL SELECT Date FROM Internet_Sales结果:[table][tr][td]Date[/td][/tr][tr][td]Jan-05-1999[/td][/tr][tr][td]Jan-07-1999[/td][/tr][tr][td]Jan-08-1999[/td][/tr][tr][td]Jan-08-1999[/td][/tr][tr][td]Jan-07-1999[/td][/tr][tr][td]Jan-10-1999[/td][/tr][tr][td]Jan-11-1999[/td][/tr][tr][td]Jan-12-1999[/td][/tr][/table]
select 中的union 和union all用法
jayli 发表于 2006-12-15 15:21:00
UNION
The UNION command is used to select related information from two tables, much like the JOIN command. However, when using the UNION command all selected columns need to be of the same data type.
Note: With UNION, only distinct values are selected.
SQL Statement 1
UNION
SQL Statement 2
Employees_Norway:
E_ID
E_Name
01
Hansen, Ola
02
Svendson, Tove
03
Svendson, Stephen
04
Pettersen, Kari
Employees_USA:
E_ID
E_Name
01
Turner, Sally
02
Kent, Clark
03
Svendson, Stephen
04
Scott, Stephen
Using the UNION Command
Example
List all different employee names in Norway and USA:
SELECT E_Name FROM Employees_Norway
UNION
SELECT E_Name FROM Employees_USA
Result
E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Scott, Stephen
Note: This command cannot be used to list all employees in Norway and USA. In the example above we have two employees with equal names, and only one of them is listed. The UNION command only selects distinct values.
UNION ALL
The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values.
SQL Statement 1
UNION ALL
SQL Statement 2
Using the UNION ALL Command
Example
List all employees in Norway and USA:
SELECT E_Name FROM Employees_Norway
UNION ALL
SELECT E_Name FROM Employees_USA
Result
E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Svendson, Stephen
Scott, Stephen
上面这个范例很好的描述了union的用法,对于union主要注意两点
union(或union all)两边的结果集的列数必须一致,相同位置的列类型必须一致 例如 select t1.column1,t1.column2...t1.columnX from testtable1 t1 union select t2.column1,t2.column2...t2.columnX from testtable2 t2