【问题标题】:from the sample database , How could I make a query where I could search all the region ( region id) where Kivuto Id is duplicated?从示例数据库中,我如何进行查询以搜索所有重复 Kivuto Id 的区域(区域 id)?
【发布时间】:2019-01-19 22:14:30
【问题描述】:

我需要获取结果中以绿色突出显示的 3 行,即单独的区域 id 但相同的 kivuto id。我需要纠正此类产品,以便我可以更正 kivuto id 的

【问题讨论】:

标签: mysql sql database group-by having-clause


【解决方案1】:

试试这个。

select * from table_name 
  where kivuto_id in (
    select email from table_name
      group by kivuto_id
      having count(*) > 1
  )

你也可以参考这个:Find rows that have the same value on a column in MySQL

【讨论】:

    【解决方案2】:

    你可以简单地使用exists:

    select t.*
    from t
    where exists (select 1
                  from t t2
                  where t2.kivuto_id = t.kivuto_id and
                        t2.region_id <> t.region_id
                 );
    

    为了提高性能,您需要在(kivuto_id, region_id) 上建立索引。

    【讨论】:

      猜你喜欢
      • 2022-07-25
      • 1970-01-01
      • 2012-04-15
      • 2022-01-04
      • 2020-07-07
      • 1970-01-01
      • 2015-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多