简体   繁体   English

具有相同计数值的两个表之间的内部联接

[英]Inner join between two tables with same count values

I have been working on this issue since 2 days now. 从两天以来,我一直在研究这个问题。

I have two tables created by using SQL Select statements 我有两个使用SQL Select语句创建的表

SELECT (


) Target

INNER JOIN

SELECT (


) Source

ON Join condition 1
AND Join condition 2
AND Join condition 3
AND Join condition 4
AND Join condition 5

The target table has count value of 10,000 records. 目标表的计数值为10,000条记录。 The source table has count value of 10,000 records. 源表的计数值为10,000条记录。

but when I do an inner join between the two tables on the 5 join conditions 但是当我在5个连接条件下在两个表之间进行内部连接时

I get 9573 records. 我得到9573条记录。

I am basically trying to find a one to one match between source and target table. 我基本上是想在源表和目标表之间找到一对一的匹配。 I feel every field from target matches every field in source. 我觉得目标的每个字段都与源代码中的每个字段相匹配。

Questions: 问题:

  1. Why does my inner join give less records even if there are same value of records in both tables? 为什么即使两个表中的记录值相同,我的内部联接也给出较少的记录?
  2. If it is expected, how can I make sure I get the exact 10,000 records after the join condition? 如果可以预期,如何确定加入条件后得到的确切10,000条记录?

There's some really good articles about the different joins out there. 有一些关于不同连接的非常好的文章。 But it looks like you'd be interested in left joins. 但是,您似乎会对左联接感兴趣。 So if it exists in Target, but not in Source, it will not drop the record. 因此,如果它存在于Target中而不存在于Source中,则不会删除该记录。

So, it would be: 因此,它将是:

SELECT(...) Target
LEFT OUTER JOIN
SELECT(...) Source
   ON cond1 and cond2 and cond3 and cond4 and cond5

Give that a shot and let me know how it goes! 试一试,让我知道如何进行!

1) An INNER JOIN only outputs the rows from the JOINING of two tables where their joining columns match. 1)INNER JOIN仅从两个表的联接列匹配的联接的表中输出行。 So in your case, Join Condition1 may not exist in rows in both tables and therefore some rows are filtering out. 因此,在您的情况下,两个表中的行都可能不存在Join Condition1,因此某些行被过滤掉了。

2) As the other poster mentioned a left join is one way. 2)正如其他海报提到的那样,左联接是一种方法。 You need to look which table source or target you want to use as your master ie start from and return all those rows. 您需要查看要用作母版的表源或目标,即从所有这些行开始并返回。 You then left join the remaining table based on your conditions to add all the columns where you join conditions match. 然后,根据您的条件将剩余的表连接起来,以添加所有连接条件匹配的列。

It's probably better if you give us the tables you are working on and the query\\results you are trying to achieve. 如果给我们您正在处理的表以及您要实现的查询\\结果,可能会更好。

Sometime you need to rely on logical analysis rather than feelings. 有时您需要依靠逻辑分析而不是情感。 Use this query to find the fields that do not match and then work out your next steps 使用此查询查找不匹配的字段,然后制定下一步

SELECT 
Target.Col1,Source.Col1,
Target.Col2,Source.Col2,
Target.Col3,Source.Col3
FROM
(

) Target
FULL OUTER JOIN
(

) Source
ON  Target.Col1=Source.Col1
AND Target.Col2=Source.Col2
AND Target.Col3=Source.Col3
WHERE (
Target.Col1 IS NULL 
OR Source.Col1 IS NULL 
OR Target.Col2 IS NULL 
OR Source.Col2 IS NULL 
OR Target.Col3 IS NULL
OR Source.Col3 IS NULL
)

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

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