【问题标题】:Finding duplicate data in MySQL database using wildcard使用通配符在 MySQL 数据库中查找重复数据
【发布时间】:2018-12-12 16:38:44
【问题描述】:

我正在努力创建一个工作查询,该查询将在输出数据库中搜索可能的重复数据。

使用常规方法不起作用,因为看起来像 123456 和 123 456 的数据应该被认为是相同的。

请你们帮我写一个搜索这些重复项的查询。

数据都在一个字段中,我们称之为“数字”。

Sample data: 
id                    | number
0                     | 123456
1                     | 124355
2                     | 123432
3                     | 123 456

Expected output:
id               | number
0                | 123456
3                | 123 456

提前致谢

【问题讨论】:

  • 请分享示例数据和您的预期输出
  • 空间是唯一的问题还是有其他情况?

标签: mysql sql database duplicates wildcard


【解决方案1】:

您可以替换空格,例如:

  select replace(number, ' ', '')  , count(*)
  from my_table 
  group by replace(number, ' ', '')

为了获得行过滤,结果 count(*) > 1

select * from my_table m 
inner join (
  select replace(number, ' ', '') my_val , count(*)
  from my_table 
  group by my_val
  having count(*) > 1

) t on  t.my_val = m.replace(number, ' ', '') 

【讨论】:

    【解决方案2】:

    我建议另一种解决方案,在正确答案中使用 @scaisEdge 中使用的替换技巧。

    SELECT 
      a.id, a.number, b.id as dup_id, b.number as dup_number 
    FROM 
      mytable a,
      mytable b
    WHERE
      a.id <> b.id and
      a.number = replace(b.number, ' ', '');
    

    这会生成一份“报告”,以确定哪个是“源”值和“重复”值。

    我创建了这个sql fiddle 来试验这两种方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-09
      • 2020-04-12
      • 1970-01-01
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      • 2012-12-18
      • 2018-05-29
      相关资源
      最近更新 更多