简体   繁体   English

使用JOIN获取数据

[英]Get data using JOIN

I have two tables having following data- 我有两个表,其中包含以下数据-

Social_Tbl Social_Tbl

ID  Name         Value
------------------------
1   Facebook     FB
2   Orkut        OR
3   Google       GL
4   Other        OT

And Organization_tbl 和Organization_tbl

ID  Organization   Name  
-----------------------------      
1   1234           Facebook
2   1234           Google
3   146            Other
4   126            Other
5   126            Facebook
6   77             Google

Here, 'Name' is the foreign key (Not ID). 在这里,“名称”是外键(不是ID)。
I want to join these tables and get the 'Name' columns data which does not belong to organization id 1234. As follows- 我想加入这些表并获取不属于组织ID 1234的“名称”列数据。

Name
----
Orkut 
Other

Here, 'Orkut' and 'Other' does not belong to 1234 organization. 在这里,“ Orkut”和“ Other”不属于1234组织。

I tried following query for this- 我尝试了以下查询-

select * from Social_Tbl st
join Organization_tbl ot 
     on st.Name = ot.Name
where Organization = 1234

This query fetches Names related to 1234 ie Facebook and Google. 该查询获取与1234相关的名称,即Facebook和Google。 I want result Orkut and Other. 我想要结果Orkut和其他。 If I replace Organization = 1234 with Organization != 1234 it returns all data from Organization_tbl. 如果我将Organization = 1234替换为Organization != 1234它将返回Organization_tbl的所有数据。

Can somebody help me on this. 有人可以帮我这个忙吗? This should be pretty simple, just npt able to find it out. 这应该非常简单,只需npt就能找出来。

Could be done with a subquery: 可以通过子查询来完成:

select st.Name
from Social_Tbl st
where not exists (
    select *
    from Organization_tbl ot
    where st.Name = ot.Name
      and ot.Organization = 1234
)

(This also returns names that don't have an entry in Organization_tbl at all.) (这还会返回在Organization_tbl中完全没有条目的名称。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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