繁体   English   中英

使用JOIN获取数据

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM