資料庫 join的解析

2021-06-06 12:56:22 字數 950 閱讀 1415

關於左連線和右連線總結:

左連線where隻影向右表,右連線where只影響左表。

(1) left join 

select * from table1 left join table2 where table1.id = table2.id

左連線後的檢索結果是顯示table1的所有資料和table2中滿足where 條件的資料。

簡言之 left join影響到的是右邊的表

左邊的為主表,右邊為從屬表

例如:select a.cust_id ,b.order_date,b.tot_ant

from customer a left join sales b

on (a.cust_id =b.cust_id and b.order_date>''1996/10/15'')

也可以寫為

select a.cust_id,b.order_date,b.tot_ant

from custom a 

left join (select * from sales where order_date>''1996/10/15'') b

on a.cust_id =b.cust_id

(2) right join

select * from

table1 right jointable

2 wheretable1.id = table2.id

檢索結果是table2的所有資料和table1中滿足where 條件的資料。

簡言之 right join影響到的是左邊的表。

左邊的表為從屬表,右邊的表為主表

inner join

select * from tbl1 inner join tbl2 on tbl1.id = tbl2.id

功能和 select * from tbl1,tbl2 where tbl1.id=tbl2.id相同。

資料庫 資料庫表連線Join

一條sql join語句對應著關係代數裡的乙個join操作,它對關聯式資料庫裡乙個或多個表的列進行合併。ansi標準的sql規定了5種型別的join inner,left outer,right outer,full outer和cross。除此之外乙個表能夠對自身進行連線,即self join。以...

實現資料庫的跨庫join

科技優家 2016 12 27 15 49 首先要理解原始需求是什麼,為什麼要跨庫join。舉個簡單的例子,在日誌資料庫log db有乙份充值記錄表pay log,裡面的使用者資訊只有乙個userid 而使用者的詳細資訊放在主庫main db,裡面有使用者的詳細資訊表user info,如使用者名稱...

資料庫中 join 的使用

關聯多張表資料,並通過條件篩選符合條件的資料。一般來說,會有四種 1.left join 左連線 2.right join 右連線 3.inner join 內連線 4.full join 全連線 left join以左表作為基準,然後加入符合條件的右表資料,最終得到的資料數目基本會不小於左表的資料...