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

     发现在mysql中会报错

    错误信息:[Err] 1093 - You can't specify target table 'people' for update in FROM clause

    于是查资料说是不能先select出同一表中的某些值,再update这个表(在同一语句中) 

    上诉实例2在mysql会报错 mssql和oracle不会 可以改为:

    DELETE
    FROM
        people
    WHERE
        peopleId NOT IN (
        SELECT
            peopleId
        FROM
            (
                SELECT
                    max(b.peopleId) AS peopleId
                FROM
                    `people` b
    						
                GROUP BY
                    b.peopleName
            ) b
    )
    AND time<1528560157
    

      

    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
    
  • 相关阅读:
    NAMESPACE
    所谓has a 和 is a
    C++ 的多重继承
    c# 与 c++ 编译
    初始化的顺序:和定义的顺序以及初始化函数都有关系。都要先定义的在前,后定义的在后。甚至连类的顺序都必须这样。
    关于转换
    隐藏
    第四章第四个例题(LRJ)
    初来扎到啊(觉得有些神圣尼)
    理解JS的执行环境
  • 原文地址:https://www.cnblogs.com/mracale/p/9146260.html
Copyright © 2020-2023  润新知