[英]Get data using JOIN
我有兩個表,其中包含以下數據-
Social_Tbl
ID Name Value
------------------------
1 Facebook FB
2 Orkut OR
3 Google GL
4 Other OT
和Organization_tbl
ID Organization Name
-----------------------------
1 1234 Facebook
2 1234 Google
3 146 Other
4 126 Other
5 126 Facebook
6 77 Google
在這里,“名稱”是外鍵(不是ID)。
我想加入這些表並獲取不屬於組織ID 1234的“名稱”列數據。
Name
----
Orkut
Other
在這里,“ Orkut”和“ Other”不屬於1234組織。
我嘗試了以下查詢-
select * from Social_Tbl st
join Organization_tbl ot
on st.Name = ot.Name
where Organization = 1234
該查詢獲取與1234相關的名稱,即Facebook和Google。 我想要結果Orkut和其他。 如果我將Organization = 1234
替換為Organization != 1234
它將返回Organization_tbl的所有數據。
有人可以幫我這個忙嗎? 這應該非常簡單,只需npt就能找出來。
可以通過子查詢來完成:
select st.Name
from Social_Tbl st
where not exists (
select *
from Organization_tbl ot
where st.Name = ot.Name
and ot.Organization = 1234
)
(這還會返回在Organization_tbl
中完全沒有條目的名稱。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.