SQL 聯合查詢

2021-06-02 08:18:00 字數 1285 閱讀 3602

use xsgl

go select * from student

select * from cause

select * from exam

--聯合查詢 join on (預設為inner,如果有right or left 那麼就指的是外聯,outer 可以不寫)

/* 1.最長見為內聯 table1 inner join table2 on table1.id = table2.id (inner 可以省略)

當且僅當兩個表中都有一行符合連線條件時才會顯示

2.外聯

-- 左外聯 table1 left outer join table2 on table1.id = table2.id (outer 可以省略) 查詢出table1 表中所有的項,即使table2中與之對應的不存在

-- 右外聯 table1 right outer join table2 on table1.id = table2.id (outer 可以省略) 查詢出 table2 表中所有的項,即使table2 中與之對應的不存在

-- 全外聯 table1 full outer join table2 on table1.id= table2.id (outer 可以省略) 查詢兩個表中的所有項,含表中與另乙個不對應的項

-- 交叉聯接 table1 cross join table2 (沒有outer 也沒有 on,可以用where ) 返回第乙個表的每一行與第二表的所有行組成的表

*/select student.id,student.name,student.class, exam.grade from student inner join exam on student.id = exam.stu_id -- 內聯

select student.id,student.name,student.class,exam.grade from student left outer join exam on student.id = exam.stu_id -- 左外聯

select student.id,student.name,student.class,exam.grade from student right outer join exam on student.id = exam.stu_id --右外聯

select student.id,student.name,student.class,exam.grade from student cross join exam where student.id = exam.stu_id -- 交叉聯合

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

sql 聯合查詢

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