Left Join和Inner Join的體會

2021-05-11 14:14:02 字數 3520 閱讀 4565

left join

和inner join的體會

有字典表zs_dic

belongtable

所屬資料庫表

belongfield

所屬字段

fieldvalue

記錄值displayvalue

顯示值

有下面乙個sql語句,zs_contract表的pro_property欄位和qualification欄位的顯示值記錄在字典表中,現在查詢的時候要查詢它們的顯示值

select dbo.zs_contract.pro_name, dbo.zs_contract.build_com, dbo.zs_contract.con_prise,

dbo.zs_contract.con_no,

dbo.zs_design_sheet.sheet_no,

signtime,

zs_dic_1.displayvalue as qualification,

dbo.zs_dic.displayvalue as pro_property

from dbo.zs_contract inner join

dbo.zs_design_sheet on dbo.zs_contract.id = dbo.zs_design_sheet.id

inner join

dbo.zs_dic on dbo.zs_contract.pro_property = dbo.zs_dic.fieldvalue

inner join

dbo.zs_dic zs_dic_1 on dbo.zs_contract.qualification = zs_dic_1.fieldvalue

where

(zs_dic_1.belongtable = 'zs_contract') and (zs_dic_1.belongfield = 'qualification')

and

(dbo.zs_dic.belongtable = 'zs_contract') and(dbo.zs_dic.belongfield = 'pro_property') (

注意這裡)

and

(dbo.zs_design_sheet.design_id = '08dee415-eeb3-4f0d-a923-b9a38c6793af')

查詢關係如下圖

但是由於種種原因,pro_property欄位的值允許輸入null值,結果導致上面的sql語句查詢值為空。後將

inner join

dbo.zs_dic on dbo.zs_contract.pro_property = dbo.zs_dic.fieldvalue

改為:left join

dbo.zs_dic on dbo.zs_contract.pro_property = dbo.zs_dic.fieldvalue

仍然無效。

後來經過思考,(dbo.zs_dic.belongtable = 'zs_contract') and (dbo.zs_dic.belongfield = 'pro_property') 語句是對pro_property的限定,要找滿足belongtable = 'zs_contract'和zs_dic.belongfield = 'pro_property'的且pro_property字段值等於fieldvalue的displayvalue。zs_contract為左表,zs_dic為右表,而left jion的作用現在只對dbo.zs_dic on dbo.zs_contract.pro_property = dbo.zs_dic.fieldvalue起作用,而belongtable = 'zs_contract'和zs_dic.belongfield = 'pro_property'是在where後面的,表示一定要滿足,但是如果滿足where的條件,則fieldvalue只能為非null的值,自然就查不到了。後將**改為:

select dbo.zs_contract.pro_name, dbo.zs_contract.build_com, dbo.zs_contract.con_prise,

dbo.zs_contract.con_no,

dbo.zs_design_sheet.sheet_no,

signtime,

zs_dic_1.displayvalue as qualification,

dbo.zs_dic.displayvalue as pro_property

from dbo.zs_contract inner join

dbo.zs_design_sheet on dbo.zs_contract.id = dbo.zs_design_sheet.id

left join

dbo.zs_dic on dbo.zs_contract.pro_property = dbo.zs_dic.fieldvalue

and

(dbo.zs_dic.belongtable = 'zs_contract')

and(dbo.zs_dic.belongfield = 'pro_property') (

修改後)

inner join

dbo.zs_dic zs_dic_1 on dbo.zs_contract.qualification = zs_dic_1.fieldvalue

where

(zs_dic_1.belongtable = 'zs_contract') and (zs_dic_1.belongfield = 'qualification')

and

(dbo.zs_design_sheet.design_id = '08dee415-eeb3-4f0d-a923-b9a38c6793af')

將where後面的條件移到left join裡,則查詢正常,可以滿足pro_property為null或不為null的情況。

由此得出經驗:在上例中,如果要對右表做出一些非列關聯的條件過濾,例如要求某列值等於乙個固定的值(如:zs_dic.belongfield = 'pro_property'),如果右表中可能出現與左表不匹配的資料,除了用left join外,還要注意不要將這些過濾條件放在where後面,而應該放在left join後面。

另外,使用where語句時應注意區分左右表的關聯方式,在where語句中注意過濾的條件,見下表

關聯方式

注意的問題

a left join b

不要在where語句中加入對b表的過濾,在left jion後面加

a right join b

不要在where語句中加入對a表的過濾,在right join 後面加

a inner join b ——

資料庫多表查詢之 where和INNER JOIN

select a.id,b.name,b.date from customers a,sales b where a.id b.id select a.id,b.name,b.date from customers a inner join sales b on a.id b.id inner jo...

left join和inner join的區別

舉例a b表 bidbnum120 230aidanum110 220330 left join以左表為準 select from a left join b on a.aid b.bid aidanum bidbnum110 120220 230330 null null inner join 選...

left join 和 right join的區別

left join 會查詢出左表所有的資料,以及右錶能連線上的字段 right join 會查詢出右表所有的資料,以及左表能連線上的字段 比方說,我們有兩張表 一張是人物表person 一張是年齡表age person id 姓名 01 張三 02 李四 03 王五 另一張id age 02 20 ...