为什么我的inner join
返回的记录多于查询的最大表?
此查询返回69 899条记录:
select count(*)
from table_1@a_connection dd
inner join table_2@a_connection pr
on dd."ProjectRef" = pr."ProjectRef"
此查询返回29 486条记录
select count(*)
from table_1@a_connection
此查询返回179条记录
select count(*)
from table_2@a_connection
在MySql数据库上使用dblink查询这两个表。
答案 0 :(得分:2)
获取您希望返回的行数:
select sum(dd.cnt * pr.cnt)
from (select "ProjectRef", count(*) as cnt
from table_1@a_connection
group by "ProjectRef"
) dd join
(select "ProjectRef", count(*) as cnt
from table_2@a_connection
group by "ProjectRef"
) pr
on dd."ProjectRef" = pr."ProjectRef";
我很确定这会返回69,899。您可以检查子查询的结果,以了解重复项目引用的情况。
答案 1 :(得分:2)
为什么我的内部联接返回的记录多于最大的表 查询?
因为第一个表格或第二个表格或两者都包含ProjectRef
列中的重复值
请看一下这个简单的示例,看看可能导致这种情况的原因:http://sqlfiddle.com/#!9/fcd040/3
select * from a;
| a_id | a_name |
|------|--------|
| 1 | A1 |
| 1 | A2 |
| 1 | A3 |
select * from b;
| b_id | b_name |
|------|--------|
| 1 | B1 |
| 1 | B2 |
| 1 | B3 |
select * from a
join b
on a.a_id = b.b_id;
| a_id | a_name | b_id | b_name |
|------|--------|------|--------|
| 1 | A1 | 1 | B1 |
| 1 | A2 | 1 | B1 |
| 1 | A3 | 1 | B1 |
| 1 | A1 | 1 | B2 |
| 1 | A2 | 1 | B2 |
| 1 | A3 | 1 | B2 |
| 1 | A1 | 1 | B3 |
| 1 | A2 | 1 | B3 |
| 1 | A3 | 1 | B3 |
此示例说明了为什么连接的结果可以提供比包含最大表的记录多得多的记录。