zoukankan      html  css  js  c++  java
  • MySQL中删除重复数据只保留一条

    MySQL中删除重复数据只保留一条

     

    用SQL语句,删除掉重复项只保留一条

    在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 
    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
  • 相关阅读:
    eclipse安装Genymotion插件
    [Eclipse插件] 安装和使用JD-Eclipse插件
    [Eclipse插件] Eclipse中如何安装和使用GrepCode插件
    [Android Studio] Android Studio如何删除module(转载)
    前端html基本标签
    python实现简单FTP
    文件的md5计算
    suprocess模块
    几个小例子
    configparser模块
  • 原文地址:https://www.cnblogs.com/nankeyimengningchenlun/p/9531093.html
Copyright © 2011-2022 走看看