Can I extract the extract records that are duplicated in sql?

前端 未结 3 875
眼角桃花
眼角桃花 2021-01-23 20:04

In my table I have the following columns:

id|name|gender

I would like to extract all the names which are duplicated, but I don\'t want to scan

3条回答
  •  醉酒成梦
    2021-01-23 20:21

    SELECT
       name
    FROM
       Mytable
    GROUP BY
       name
    HAVING
       COUNT(*) > 1
    

    To separate gender too, change the GROUP BY to name, gender

    To delete duplicate rows

    DELETE MyTable
    WHERE ID NOT IN
       (
        SELECT
           MIN(Id)
        FROM
           Mytable
        GROUP BY
           name
       )
    

提交回复
热议问题