leftjoin多個on條件_sql 表連線on後面加where和and的區別
阿新 • • 發佈:2020-12-18
技術標籤:leftjoin多個on條件select 加條件sql join on 多表連線sql where between 和 andsql語句怎麼連線兩個表提交資料裡面有and就報錯
create table testA( Id number(10) primary key, Name varchar(10))insert into testA values(1,'小黃');insert into testA values(2,'小綠');insert into testA values(3,'小白');insert into testA values(4,'小黑');insert into testA values(5,'小花');commitcreate table testB( Id number(10) primary key, age number(10))insert into testB values(1,10);insert into testB values(2,11);insert into testB values(3,12);insert into testB values(4,13);commit
on後面加and
select * from testA a left join testB b on a.id = b.id and b.age=10
on後面加where
select * from testA a left join testB b on a.id = b.id where b.age=10
on 是用於消除笛卡兒積的,表連線時不加on會報錯,left join語句會從左表那裡返回所有的行,即使在右表中沒有匹配的行,on後面接and也會兼顧左連線,不管and 後面接什麼內容,左表資料都會全部展示下圖語句由於a.name =10在testA沒有匹配資料,所以與其關聯的testB也匹配不到資料。
select * from testA a left join testB b on a.id = b.id and a.name='10'
使用where就是對連線後的結果集進行條件篩選
select*fromtestAaleftjointestBbona.id=b.idwherea.name='10'