我知道这是一个相当简单的问题,但是自从我查看SQL以来已经有好几年了。我有两个包含信息的表..
商业伙伴表
Partner
1
2
3
和表业务关系
RelationshipNum Partner1 Partner2 Relationshiptype
000001 1 2 OCDL
我的查询应采用Partner1和Partner2的值,并搜索业务合作伙伴中的合作伙伴字段。如果业务关系中同一条目上的两个值都存在,则应返回业务关系中的整行。如果只有一个或没有,我们可以忽略它。
这是我到目前为止所得到的,但它没有返回任何值,我已经测试了excel中的数据集(太大而不是一个可行的选项)并且确实存在匹配。
SELECT [Business Relationships].*
FROM [Business Relationships]
INNER JOIN [Business Partners] ON ([Business Partners].Partner = [Business Relationships].Partner1)
AND ([Business Partners].Partner = [Business Relationships].Partner2);
答案 0 :(得分:4)
尝试使用以下两个INNER JOIN
SELECT BR.*
FROM ([Business Relationships] BR
INNER JOIN [Business Partners] BP1 ON BP1.[Partner] = BR.[Partner1])
INNER JOIN [Business Partners] BP2 ON BP2.[Partner] = BR.[Partner2]
答案 1 :(得分:1)
使用double EXISTS
查找要返回的行:
SELECT BR.*
FROM [Business Relationships] BR
WHERE EXISTS (select 1 from [Business Partners] where Partner = BR.Partner1)
AND EXISTS (select 1 from [Business Partners] where Partner = BR.Partner2)
永远不会返回任何重复! (根据数据,可以使用哪些JOIN解决方案。)