SQL聯合查詢

2022-03-31 12:17:59 字數 955 閱讀 4135

集合運算

基合運算子可以用於從多張表中選擇資料。

①union運算

用於求兩個結果集合的並集(兩個結果集合的所有記錄),並自動去掉重複行。

select ename,sal from account where sal>2000

union

select ename,sal from research where sal>2000

union

select ename,sal from sales where sal>2000;

注:ename,sal 是必須一致的。 

②union all運算

用於求兩個結果集合的並集(兩個結果集中的所有記錄),並且不去掉重複行。

select ename,sal from account where sal>2000

union

select ename,sal from research where sal>2000

union

select ename,sal from sales where sal>2000;

③intersect運算

intersect運算返回查詢結果中相同的部分。

各部門中有哪些相同的職位?

select job from account

intersect

select job from research

intersect

select job from sales;

④minus運算

minus返回兩個結果集的差集。(在第乙個結果集中存在的,而在第二個結果集中不存在的行。)

有那些職位是財務部中有,而在銷售部門中沒有?

select job from account

minus

select job from sales;

其他補充:

SQL 聯合查詢

use xsgl go select from student select from cause select from exam 聯合查詢 join on 預設為inner,如果有right or left 那麼就指的是外聯,outer 可以不寫 1.最長見為內聯 table1 inner jo...

sql聯合查詢

sql查詢 多表聯合查詢 將具有相同的字段的查詢結果合併為乙個表 關鍵字 union 例項 查詢subs表 select subs id,prefix,acc nbr,cust id,user id,acct id,price plan id,area id,update date from sub...

SQL 聯合查詢

a表 aaa bbb ccc 1a 1b 1c 2a 2b 2c 3a 3b 3c b表 aaa bbb ddd 1a 1b 1d 4a 4b 4d 1 union union all all 表示將查詢的所有結果都合併到結果集中,若不加all會將重複的行只保留一行 sql view plain c...