查找具有重复/相似列值的行MySQL [英] Find rows with duplicate/similar column values MySQL

查看:110
本文介绍了查找具有重复/相似列值的行MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从下表中选择在fname列中具有与其顺序中的第一个相似的值的所有行。 IOW从这个表我想要检索ids 2,5和7的行(因为安娜来自安娜 michaela 和michaal 来自迈克尔)。

I want to select from the following table all the rows which have similar values in the fname column as the first in their order. IOW from this table I want to retrieve rows with ids 2,5 and 7 (because " anna" comes after "anna", and "michaela" and "michaal" come after "michael").

+----+------------+----------+
| id | fname      | lname    |
+----+------------+----------+
|  1 | anna       | milski   |
|  2 |  anna      | nguyen   |
|  3 | michael    | michaels |
|  4 | james      | bond     |
|  5 | michaela   | king     |
|  6 | bruce      | smart    |
|  7 | michaal    | hardy    |
+----+------------+----------+

我到目前为止是这样的:

What I have so far is this:

select *, count(fname) cnt 
from users group by soundex(fname) 
having count(soundex(fname)) > 1;

但是,由于我对它进行分组,所以结果是

but since I'm grouping it the result is

+----+----------+----------+-----+
| id | fname    | lname    | cnt |
+----+----------+----------+-----+
|  1 | anna     | milski   |   2 |
|  3 | michael  | michaels |   3 |
+----+----------+----------+-----+

我想要检索的是:

+----+----------+----------+-----+
| id | fname    | lname    | cnt |
+----+----------+----------+-----+
|  2 |  anna    | nyugen   |   2 |
|  5 | michaela | king     |   3 |
|  7 | michaal  | hardy    |   3 |
+----+----------+----------+-----+

我该怎么改变查询?我尝试删除group by,但它会改变结果(我可能是错的,没有广泛测试)。

What should I change about the query? I tried removing "group by" but it changes the results (I could be wrong, haven't tested it extensively).

推荐答案

我已经重新阅读了你的初始问题,并提出了以下解决方案:

I've re-read your initial question and I've came up with the following solution:

SELECT *
FROM   users
WHERE  id IN
       (SELECT id
       FROM    users t4
               INNER JOIN
                       (SELECT  soundex(fname) AS snd,
                                COUNT(*)       AS cnt
                       FROM     users          AS t5
                       GROUP BY snd
                       HAVING   cnt > 1
                       )
                       AS t6
               ON      soundex(t4.fname)=snd
       )
AND    id NOT IN
       (SELECT  MIN(t2.id) AS wanted
       FROM     users t2
                INNER JOIN
                         (SELECT  soundex(fname) AS snd,
                                  COUNT(*)       AS cnt
                         FROM     users          AS t1
                         GROUP BY snd
                         HAVING   cnt > 1
                         )
                         AS t3
                ON       soundex(t2.fname)=snd
       GROUP BY snd
       );

有点过于复杂,但它工作并提供您所要求的:)

It's a bit over-complicated, but it works and delivers exactly what you asked for :)

这篇关于查找具有重复/相似列值的行MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆