sql聯合查詢

2021-06-06 06:51:48 字數 1588 閱讀 8614

sql查詢:

多表聯合查詢:將具有相同的字段的查詢結果合併為乙個表

關鍵字:union

例項:查詢subs表

select subs_id,prefix,acc_nbr,cust_id,user_id,acct_id,price_plan_id,area_id,update_date

from subs

where acc_nbr=939106638 --acc_nbr

結果:

1 432 95939106638 197197 248124 1202012/1/10 12:07:43

2 439 95939106638 200200 250124 1202012/1/10 13:16:50

3 425 95 939106638 199 199 247 124 120 2012/1/10 10:15:24

4 409 95939106638 199199 247124 1202012/1/9 13:46:43

查詢subs_his表:

select subs_id,prefix,acc_nbr,cust_id,user_id,acct_id,price_plan_id,area_id,update_date

from subs_his

where acc_nbr=939106638 --acc_nbr

結果:

1414959391066381991992471241202012/1/9 14:28:20

2414959391066381991992471241202012/1/9 14:00:38

聯合查詢結果:

語句:

select subs_id,prefix,acc_nbr,cust_id,user_id,acct_id,price_plan_id,area_id,update_date

from subs

where acc_nbr=939106638 --acc_nbr

union select subs_id,prefix,acc_nbr,cust_id,user_id,acct_id,price_plan_id,area_id,update_date

from subs_his

where acc_nbr=939106638 --acc_nbr

查詢結果:

1409959391066381991992471241202012/1/9 13:46:43

2414959391066381991992471241202012/1/9 14:00:38

3414959391066381991992471241202012/1/9 14:28:20

4425959391066381991992471241202012/1/10 10:15:24

5432959391066381971972481241202012/1/10 12:07:43

6439959391066382002002501241202012/1/10 13:16:50

。。

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 聯合查詢

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...

sql 聯合查詢

概述 聯合查詢效率較高,舉例子來說明聯合查詢 內聯inner join 左聯left outer join 右聯right outer join 全聯full outer join 的好處及用法。聯合查詢效率較高,以下例子來說明聯合查詢 內聯 左聯 右聯 全聯 的好處 t1表結構 使用者名稱,密碼 ...