Find rows with duplicate/similar column values MySQL

后端 未结 2 1500
温柔的废话
温柔的废话 2020-12-22 01:59

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

2条回答
  •  旧巷少年郎
    2020-12-22 02:43

    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 :)

提交回复
热议问题