SQL基本操作 UNION

2022-07-14 17:00:28 字數 1312 閱讀 9419

union 操作符:用於合併兩個或多個 select 語句的結果集。請注意,union 內部的 select 語句必須擁有相同數量的列。列也必須擁有相似的資料型別。同時,每條 select 語句中的列的順序必須相同。

注釋:預設地,union 操作符選取不同的值。如果允許重複的值,請使用 union all。另外,union 結果集中的列名總是等於 union 中第乙個 select 語句中的列名。

employees_china表:

e_id

e_name

01zhang, hua

02wang, wei

03carter, thomas

04yang, ming

employees_usa表:

e_id

e_name

01adams, john

02bush, george

03carter, thomas

04gates, bill

使用 union 命令:列出所有在中國和美國的不同的雇員名

select e_name from

employees_china

union

select e_name from employees_usa

e_name

zhang, hua

wang, wei

carter, thomas

yang, ming

adams, john

bush, george

gates, bill

注釋:這個命令無法列出在中國和美國的所有雇員。在上面的例子中,我們有兩個名字相同的雇員,他們當中只有乙個人被列出來了。union 命令只會選取不同的值。

使用 union all 命令:列出在中國和美國的所有的雇員:

select e_name from

employees_china

union

allselect e_name from employees_usa

e_name

zhang, hua

wang, wei

carter, thomas

yang, ming

adams, john

bush, george

carter, thomas

gates, bill

參考:sql union 和 union all 操作符

SQL之union操作符

菜鳥教程 sqlunion操作符合併兩個或多個 select 語句的結果 union 結果集中的列名總是等於union 中第乙個 select 語句中的列名 不允許重複 union select column name s from table1 union select column name s...

SQL的union操作符

union 操作符用於合併兩個或多個 select 語句的結果集。請注意,union 內部的 select 語句必須擁有相同數量的列。列也必須擁有相似的資料型別。同時,每條select 語句中的列的順序必須相同。select column name s from table name1 union ...

SQL基本操作

create database mydatabase1 on primary 配置主資料檔案的選項 name mydatabase2 主資料檔案的邏輯名稱 filename d database database1 mydatabase1.mdf 主資料檔案的實際儲存路徑 size 5mb,主檔案的...