mysql去重复 保留一条

作者:原创时间:2022-03-10
文档

mysql去重复 保留一条怎么操作呢?一起来看下吧:

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断

SELECT
    *
FROM
    people
WHERE
    peopleId IN (
        SELECT
            peopleId
        FROM
            people
        GROUP BY
            peopleId
        HAVING
            count(peopleId) > 1
    )

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录

DELETE
FROM
    people
WHERE
    peopleName IN (
        SELECT
            peopleName
        FROM
            people
        GROUP BY
            peopleName
        HAVING
            count(peopleName) > 1
    )
AND peopleId NOT IN (
    SELECT
        min(peopleId)
    FROM
        people
    GROUP BY
        peopleName
    HAVING
        count(peopleName) > 1
)

3、查找表中多余的重复记录(多个字段)

SELECT
    *
FROM
    vitae a
WHERE
    (a.peopleId, a.seq) IN (
        SELECT
            peopleId,
            seq
        FROM
            vitae
        GROUP BY
            peopleId,
            seq
        HAVING
            count(*) > 1
    )

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录

DELETE
FROM
    vitae a
WHERE
    (a.peopleId, a.seq) IN (
        SELECT
            peopleId,
            seq
        FROM
            vitae
        GROUP BY
            peopleId,
            seq
        HAVING
            count(*) > 1
    )
AND rowid NOT IN (
    SELECT
        min(rowid)
    FROM
        vitae
    GROUP BY
        peopleId,
        seq
    HAVING
        count(*) > 1
)

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录

SELECT
    *
FROM
    vitae a
WHERE
    (a.peopleId, a.seq) IN (
        SELECT
            peopleId,
            seq
        FROM
            vitae
        GROUP BY
            peopleId,
            seq
        HAVING
            count(*) > 1
    )
AND rowid NOT IN (
    SELECT
        min(rowid)
    FROM
        vitae
    GROUP BY
        peopleId,
        seq
    HAVING
        count(*) > 1
)

6、消除一个字段的左边的第一位:

UPDATE tableName
SET [ Title ]= RIGHT ([ Title ],(len([ Title ]) - 1))
WHERE
    Title LIKE '村%'

7、消除一个字段的右边的第一位:

UPDATE tableName
SET [ Title ]= LEFT ([ Title ],(len([ Title ]) - 1))
WHERE
    Title LIKE '%村'

8、假删除表中多余的重复记录(多个字段),不包含rowid最小的记录

UPDATE vitae
SET ispass =- 1
WHERE
    peopleId IN (
        SELECT
            peopleId
        FROM
            vitae
        GROUP BY
            peopleId

以上就是小编今天的分享,希望可以帮助到大家。

显示全文
rstudio使用教程 oracle coalesce python configparser模块 python strip函数 python调用dll java concat js 遍历json js json对象 js数组遍历方法 python 引用 idea的maven配置 maven安装配置 java filereader java stream java random java lambda java hashmap java foreach java final java json js set方法 python中get函数用法 java get和set方法 object转bigdecimal object 转list python 交集 js读取文件 linux 格式化u盘 deque java java process armoury crate打不开 python skimage vue使用jquery js set去重 python的range函数用法 vue 调用子组件方法 java反射获取属性值 python中find函数的用法 getclass方法 python中strip函数的用法