資料庫左連線的一點知識

2022-06-24 10:48:12 字數 4449 閱讀 6564

資料庫的做鏈結我們經常遇到,有一次發現了順序問題,沒有搞明白,現在在回顧總結下。

首先建立2張表a,b,然後插入初始化資料。

create

table a(id int

);create

table b(id int

);insert

into a values(1

);insert

into a values(2

);insert

into a values(3

);insert

into b values(1

);insert

into b values(2

);insert

into b values(3

);select

*from

a;select

*from

b;id

id我們假設命名為case1.

--現在看看這個左連線

select a.*,b.*

from a a left

join

b b

on a.id =b.id and a.id=

2;

id id

----------- -----------

null

2null

這裡我們假設命名為case2

--看看下面這個

select a.*,b.*

from a a left

join

b b

on a.id =

b.id

where a.id=2;

id id

----------- -----------

2

於是出現了case1與case2兩種不同的結果,這裡說明了什麼呢?

首先我們要知道什麼是左連線:左連線的關鍵字是left join ,

left join 關鍵字會從左表 (a) 那裡返回所有的行,即使在右表 (b) 中沒有匹配的行,顯示為空值(null)。

case1沒有用到where篩選,case2用到了where篩選

在使用left jion時,on和where條件的區別如下: 

1、 on條件是在生成臨時表時使用的條件,它不管on中的條件是否為真,都會返回左邊表中的記錄。

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

( left join:一旦加上where條件,則顯示的結果等於inner join)

其實以上結果的關鍵原因就是left join,right join,full join的特殊性,不管on上的條件是否為真都會返回left或right表中的記錄,full則具有left和right的特性的並集。

而inner join沒這個特殊性,則條件放在on中和where中,返回的結果集是相同的。

例如,

select a.*,b.*

from a a left

join

b b

on a.id =b.id and a.id=

4;

id id

null

null

null

這裡看到,用and的時候,不管條件為真還是假都返回left或right表中的記錄

select a.*,b.*

from a a left

join

b b

on a.id =

b.id

where a.id=4;

id id

這裡沒有查詢出任何資料,

可以看出 

left join:一旦加上where條件,則顯示的結果等於inner join

select a.*,b.*

from a a inner

join

b b

on a.id =b.id and b.id=

2;

id id

22

而inner join沒這個特殊性,則條件放在on中和where中,返回的結果集是相同的。

基本查詢情況就是這樣子的。

共同學習,共同進步!

資料庫的做鏈結我們經常遇到,有一次發現了順序問題,沒有搞明白,現在在回顧總結下。

首先建立2張表a,b,然後插入初始化資料。

create

table a(id int

);create

table b(id int

);insert

into a values(1

);insert

into a values(2

);insert

into a values(3

);insert

into b values(1

);insert

into b values(2

);insert

into b values(3

);select

*from

a;select

*from

b;id

id我們假設命名為case1.

--現在看看這個左連線

select a.*,b.*

from a a left

join

b b

on a.id =b.id and a.id=

2;

id id

----------- -----------

null

2null

這裡我們假設命名為case2

--看看下面這個

select a.*,b.*

from a a left

join

b b

on a.id =

b.id

where a.id=2;

id id

----------- -----------

2

於是出現了case1與case2兩種不同的結果,這裡說明了什麼呢?

首先我們要知道什麼是左連線:左連線的關鍵字是left join ,

left join 關鍵字會從左表 (a) 那裡返回所有的行,即使在右表 (b) 中沒有匹配的行,顯示為空值(null)。

case1沒有用到where篩選,case2用到了where篩選

在使用left jion時,on和where條件的區別如下: 

1、 on條件是在生成臨時表時使用的條件,它不管on中的條件是否為真,都會返回左邊表中的記錄。

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

( left join:一旦加上where條件,則顯示的結果等於inner join)

其實以上結果的關鍵原因就是left join,right join,full join的特殊性,不管on上的條件是否為真都會返回left或right表中的記錄,full則具有left和right的特性的並集。

而inner join沒這個特殊性,則條件放在on中和where中,返回的結果集是相同的。

例如,

select a.*,b.*

from a a left

join

b b

on a.id =b.id and a.id=

4;

id id

null

null

null

這裡看到,用and的時候,不管條件為真還是假都返回left或right表中的記錄

select a.*,b.*

from a a left

join

b b

on a.id =

b.id

where a.id=4;

id id

這裡沒有查詢出任何資料,

可以看出 

left join:一旦加上where條件,則顯示的結果等於inner join

select a.*,b.*

from a a inner

join

b b

on a.id =b.id and b.id=

2;

id id

22

而inner join沒這個特殊性,則條件放在on中和where中,返回的結果集是相同的。

基本查詢情況就是這樣子的。

共同學習,共同進步!

資料庫的一點知識

1 儲存過程是一些預編譯的sql語句,直白的理解就是 是乙個記錄集,它是由一些t sql語句組成的 塊,這些t sql語句 像乙個方法一樣實現一些功能 單錶和多表的基本操作 然後再給這個 塊取個名字。2 優點 儲存過程是乙個預編譯的 塊,執行效率比較高 乙個儲存過程替代大量t sql語句 可以降低網...

彙編一點知識

bss段 bss段 bsssegment 通常是指用來存放程式中未初始化的全域性變數的一塊記憶體區域。bss是英文blockstarted by symbol的簡稱。bss段屬於靜態記憶體分配。data段 資料段 datasegment 通常是指用來存放程式中已初始化的全域性變數的一塊記憶體區域。資...

關於wampserver的一點知識

1.wamp windows apache mysql php,一組法國人開發的用來搭建動態 或者伺服器的開源軟體。安裝極為簡便。可選擇中文語言。在瀏覽器中輸入 http localhost 站點目錄預設是www。2.關於url重寫的配置問題 url重寫 配置步驟 1 開啟apache的httpd....