简体   繁体   English

内连接返回更多行然后常规选择

[英]Inner join returning more rows then regular select

I have a query like this: 我有这样的查询:

SELECT id, run_date, feed_type_id, text 
FROM myTable
WHERE run_date >= ('20140506', 'yyyymmdd') AND run_date < ('20140506', 'yyyymmdd') + 1

This returns me 230k rows. 这将返回230k行。

When I wrap it with a WITH CLAUSE: 当我用WITH CLAUSE包装它时:

WITH daily_run as (
  SELECT     /* MATERIALZE */
    id, run_date, feed_type_id, text 
  FROM myTable
  WHERE run_date >= ('20140506', 'yyyymmdd') AND run_date < ('20140506', 'yyyymmdd') + 1)

SELECT 
id, run_date, feed_type_id, text from daily_run run; 

This also returns me 230k rows. 这也给我带来了230k行。

However when I do a join: 但是,当我加入时:

WITH daily_run as (
SELECT /* MATERIALZE */
id, run_date, feed_type_id, text 
FROM myTable
WHERE run_date >= ('20140506', 'yyyymmdd') AND run_date < ('20140506', 'yyyymmdd') + 1)

SELECT 
run.id, run.run_date, run.feed_type_id, run.text 
FROM daily_run run 
  INNER JOIN feed_id_types types 
  ON run.feed_type_id = types.feed_type_id

I get an increase of 50k rows. 我增加了5万行。 This increase in rows happens no matter what date I run it for (not always 50k). 无论我运行它的日期(不总是50k),行的增加都会发生。

The other confusing part is replacing the join with a different condition: 另一个令人困惑的部分是用不同的条件替换连接:

SELECT 
  run.id, run.run_date, run.feed_type_id, run.text 
FROM daily_run run 
WHERE run.feed_type_id in (SELECT types.feed_type_id FROM feed_id_types types)

Returns the correct 230k number. 返回正确的230k数字。

The table feed_id_types has 19 rows, and I want to join it to determine if I need to process that particular run. 表feed_id_types有19行,我想加入它以确定是否需要处理该特定运行。

Is there something I am missing from my join condition? 我的加入条件中是否有一些我遗漏的东西?

You have duplicate rows in feed_id_types. 您在feed_id_types中有重复的行。 Run this to find which IDs are duplicated: 运行此命令以查找重复的ID:

select
    types.feed_type_id
from feed_id_types types
group by types.feed_type_id
having count(*) > 1

The IN() clause ignores the duplicates, matching on the first one it finds. IN()子句忽略重复项,匹配它找到的第一个副本。 The inner join matches each row from daily_run to every matching row in feed_id_types , creating extra results. 内加入比赛,从各行daily_run在每一个匹配的行feed_id_types ,创造额外的效果。

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

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