[英]SQL return ONLY the id's which exist in another table
How can I return the check_type_dim_id
's from the Check_Type_Dimension
table that exist exclusively in the Check_Type_Master
table? 我怎样才能返回
check_type_dim_id
的从Check_Type_Dimension
,在存在专门表Check_Type_Master
表?
I should only be returned 5 results but I'm getting 6 because of trying to join on the names: 我应该只返回5个结果,但由于尝试加入名称而获得6个结果:
WHERE EXISTS
(SELECT check_type_id FROM check_type_master CTM WHERE
CTM.check_type_name = CTD.check_type_name)
The trouble is, the check_type_dim_id
is an auditing table and can have duplicate names. 问题在于,
check_type_dim_id
是一个审计表,并且可以具有重复的名称。
Table: Check_Type_Dimension
contains the following info: 表:
Check_Type_Dimension
包含以下信息:
Table Check_Type_Master
contains the following info: 表
Check_Type_Master
包含以下信息:
SELECT CTM.id
FROM check_type_master CTM
JOIN Check_Type_Dimension CTD
ON CTM.check_type_name= CTD.check_type_name AND CTM.check_type_id= CTD.check_type_id
If I understood correctly, that should solve your problem: 如果我正确理解,那应该可以解决您的问题:
SELECT check_type_dim_id FROM Check_Type_Dimension
WHERE check_type_dim_id IN (SELECT check_type_dim_id FROM Check_Type_Master)
Generally, you can use IN
operator with the subquery, to restrict some column to the set, returned by the subquery (it has to be one-column table). 通常,您可以对子查询使用
IN
运算符,以将某些列限制为子查询返回的集合(它必须是单列表)。
You should link the 2 tables in the EXISTS both on id & name. 您应该在IDIST和名称上都链接EXISTS中的2个表。
Because you when the Dimension table has 2 different id's with the same name ("Member Charge") then your query would give extra results. 因为您在维度表中有两个具有相同名称的不同ID(“会员费用”),所以您的查询将提供额外的结果。
Example: 例:
select check_type_dim_id
from Check_Type_Dimension CTD
where exists (
select 1
from Check_Type_Master CTM
where CTM.check_type_id = CTD.check_type_id
and CTM.check_type_name = CTD.check_type_name
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.