【问题标题】:Duplicate results returned from query when distinct is used使用 distinct 时从查询返回的重复结果
【发布时间】:2011-05-02 16:07:26
【问题描述】:

在我的当前项目中,我需要对从 SQL 返回的结果进行一些分页。我遇到了一个极端情况,其中查询可以接受标识符作为 where 子句的一部分,通常这不是问题,但在一种情况下,我们有一个标识符被传递,它与其中一个具有一对多关系查询连接的表,它在结果中返回多行。该问题已通过在查询中引入 distinct 得到解决。以下是返回一行正确结果的查询(当然所有表/字段名称都已更改):

select distinct [item_table].[item_id]
    , row_number() over (order by [item_table].[pub_date] desc, [item_table].[item_id]) as [row_num]
from [item_table]
    join [OneToOneRelationShip] on [OneToOneRelationShip].[other_id] = [item_table].[other_id]
    left join [OneToNoneOrManyRelationship] on [OneToNoneOrManyRelationship].[item_id] = [item_table].[item_id]
where [item_table].[pub_item_web] = 1
    and [item_table].[live_item] = 1
    and [item_table].[item_id] in (1404309)

但是,当我在查询中引入分页时,我发现它现在返回多行,而它应该只返回一个。我使用的分页方法如下:

select [item_id]
from (
      select distinct [item_table].[item_id]
            , row_number() over (order by [item_table].[pub_date] desc, [item_table].[item_id]) as [row_num]
      from [item_table]
            join [OneToOneRelationShip] on [OneToOneRelationShip].[other_id] = [item_table].[other_id]
            left join [OneToNoneOrManyRelationship] on [OneToNoneOrManyRelationship].[item_id] = [item_table].[item_id]
      where [item_table].[pub_item_web] = 1
            and [item_table].[live_item] = 1
            and [item_table].[item_id] in (1404309)
) as [items]
where [items].[row_num] between 0 and 100

我担心向外部查询添加 distinct 会导致返回的结果数量不正确,我不确定如何解决此问题。我查询的数据库是 MS SQL Server 2008。

【问题讨论】:

    标签: sql tsql sql-server-2008 pagination duplicates


    【解决方案1】:

    我看不到 DISTINCT 在您的第一个查询中添加任何值的位置。结果是 [item_table].[item_id] 和 [row_num]。因为 [row_num] 的值已经不同,所以 [item_table].[item_id] 和 [row_num] 的组合将是不同的。在查询中添加 DISTINCT 关键字时,不会排除任何行。

    在第二个查询中,您的结果将从 [row_num] 符合条件的子查询返回 [item_id]。如果子查询中有 where 重复的 [item_id] 值,最终结果中会有重复,但现在您不显示 [row_num] 来区分重复。

    【讨论】:

    • 在测试第一个查询时,它只返回一个带有 distinct 的行和三个没有它的行。虽然我同意你的观点,即它不应该增加任何价值,但所描述的行为是我发现的,所以这就是我必须运行的。
    【解决方案2】:

    在发布问题大约 5 分钟后,如果我按 item_id (和任何排序标准)分组(应该只是它的一个实例)应该可以解决问题。经过测试,这是我留下的查询:

    select [item_id]
    from (
          select [item_table].[item_id]
                , row_number() over (order by [item_table].[pub_date] desc, [item_table].[item_id]) as [row_num]
          from [item_table]
                join [OneToOneRelationShip] on [OneToOneRelationShip].[other_id] = [item_table].[other_id]
                left join [OneToNoneOrManyRelationship] on [OneToNoneOrManyRelationship].[item_id] = [item_table].[item_id]
          where [item_table].[pub_item_web] = 1
                and [item_table].[live_item] = 1
                and [item_table].[item_id] in (1404309)
          group by [item_table].[item_id], [item_table].[pub_date]
    ) as [items]
    where [items].[row_num] between 0 and 100
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-19
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      相关资源
      最近更新 更多