Left join 中where on 的 區別

2021-09-01 10:11:53 字數 1254 閱讀 6186

left join中where, on區別

table a(id, type):

id     type

1      1        

2      1         

3      2  

table b(id, class):

id    class

1      1

2      2

sql語句1:select a.*, b.* from a left join b on a.id = b.id and a.type = 1;

sql語句2:select a.*, b.* from a left join b on a.id = b.id and b.class = 1;

sql語句3:select a.*, b.* from a left join b on a.id = b.id where a.type = 1;

sql語句1的執行結果為:

a.id    a.type    b.id    b.class

1        1            1        1

2        1            2        2

3        2              

sql語句2的執行結果為:

a.id    a.type    b.id    b.class

1        1            1        1

2        1           

3        2           

sql語句3的執行結果為:

a.id    a.type    b.id    b.class

1        1            1        1

2        1            2        2

//此表結論:

left join 中左表的全部記錄將全部被查詢顯示,on 後面的條件對左邊的表不起作用,但對右表的限制條件將會起作用。

//此次結論:

資料庫在通過連線兩張或多張表來返回記錄時,都會生成一張中間的臨時表,然後再將這張臨時表返回給 使用者。

1、 on 條件是在生成臨時表時使用的條件,返回左邊的全部記錄。

2、where 條件是在臨時表生成好後,再對臨時表進行過濾的條件。這時已經沒有 left join 的含義(必須 返回左邊表的記錄)了,條件不為真的就全部過濾掉。

sql 中 left join 的使用

left join 是以左表為基礎,查詢右表的值。如果在右表中沒用沒有資料,則為null。這裡有三張表。線路bs line id,name id主鍵 線路段bs seg id,l id,name l id關聯線路id 配變bs dsub id,seg id,name seg id關聯線路段id 它們...

mysql 中LEFT JOIN基本用法例項

sql 中left join的含義是 如果 tbl user記錄了學生的姓名 name 和學號 id tbl score記錄了學生 有的學生考試以後被開除了,沒有其記錄 的學號 id 和考試成績 score 以及考試科目 subject 要想列印出各個學生姓名及對應的的各科總成績,則可以用sql語句...

left join 中on和where的講解

select from table1 left join table2 on table1.id table2.id where table2.name name left join on 首先是以左表為主表去連線右表生成乙個中間表 然後再有where 語句到生成的中間表中去查詢符合where條件的...