【问题标题】:remove duplicate entry in SQL database with null fields删除 SQL 数据库中具有空字段的重复条目
【发布时间】:2016-12-22 14:53:18
【问题描述】:

当有一些空字段时,我如何删除 SQL 数据库中的重复条目: 我有一个包含重复条目的表 Mesure2,其中一些具有空字段,我已经删除了其他重复条目,如下所示:

INSERT Mesures SELECT distinct * FROM Mesures2;

但仍有重复的空字段条目阻止我创建密钥:

MariaDB [sidonie2]> ALTER TABLE Mesures ADD PRIMARY KEY (`N° Fiche`,Date,Angle,Sépar,`Nb Nuits`,CodeObs,Instrument,dimension,Réf,Nota);
 ERROR 1062 (23000): Duplicate entry '2928-1892.93-258.6-03.34-2-JNS---LDS-' for key 'PRIMARY'

MariaDB [sidonie2]> select * from Mesures where `N° Fiche` = 2928 and Date = 1892.93;
  +-----------+---------+-------+--------+----------+---------+-----------+------------+------+------+
  | N° Fiche  | Date    | Angle | Sépar  | Nb Nuits | CodeObs | dimension | Instrument | Réf  | Nota |
  +-----------+---------+-------+--------+----------+---------+-----------+-----------+------+------+
  |      2928 | 1892.93 | 258.6 | 03.34  |        2 | JNS     | NULL      | NULL       | LDS  |      |
  |      2928 | 1892.93 | 258.6 | 03.34  |        2 | JNS     | NULL      | NULL       | LDS  | NULL |
  +-----------+---------+-------+--------+----------+---------+-----------+------------+------+------+

如何消除仅由 Null 和空相同字段(这里是 Nota 字段但可以是其他任何字段)不同的重复项?

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    很奇怪,您将整行定义为主键。你考虑过自增字段吗?

    无论如何,如果要删除 NULL 值,请在插入中执行:

    INSERT Mesures(`N° Fiche`, Date, Angle, Sépar, `Nb Nuits`, CodeObs, Instrument, dimension, Réf, Nota)
        SELECT distinct `N° Fiche`, Date, Angle, Sépar, `Nb Nuits`, CodeObs, Instrument, dimension, Réf, Nota
        FROM Mesures2
        WHERE `N° Fiche` IS NOT NULL AND
              Date IS NOT NULL AND
              Angle IS NOT NULL AND
              Sépar IS NOT NULL AND
              `Nb Nuits` IS NOT NULL AND
              CodeObs IS NOT NULL AND
              Instrument IS NOT NULL AND
              dimension IS NOT NULL AND
              Réf IS NOT NULL AND
              Nota IS NOT NULL;
    

    这种方法确实假设INSERT之前的表是空的。

    【讨论】:

    • 关于主键,表不是我做的,我试图改变这个,关于答案如果我这样做假设我只会得到没有空字段的行,但我必须保留所有不同的行,除非它们的不同之处在于有一个或多个与空字段重复的空字段
    • 不,这不是我想要的,这个查询将选择并插入没有 NULL 字段的不同数据,但只会删除有效数据,因为它们有一个 NULL 字段,不好。
    【解决方案2】:

    这取决于您是否要将空值保留为 NULL:

    INSERT Mesures (`N° Fiche`,Date,Angle,Sépar,`Nb Nuits`,CodeObs,Instrument,dimension,Réf,Nota)
    SELECT DISTINCT
        `N° Fiche`,
        Date,
        Angle,
        Sépar,
        `Nb Nuits`,
        CodeObs,
        Instrument,
        dimension,
        Réf,
        nullif(Nota,'')
    FROM Mesures2;
    

    或者,如果您希望 null 为空字符串:

    INSERT Mesures (`N° Fiche`,Date,Angle,Sépar,`Nb Nuits`,CodeObs,Instrument,dimension,Réf,Nota)
    SELECT DISTINCT
        `N° Fiche`,
        Date,
        Angle,
        Sépar,
        `Nb Nuits`,
        CodeObs,
        Instrument,
        dimension,
        Réf,
        ifnull(Nota,'')
    FROM Mesures2;
    

    请注意,nullif()ifnull() 是非常不同的函数。

    【讨论】:

    • 感谢您的帮助,我在字段上使用 ifnull()(或 nullif)。
    【解决方案3】:
    alter IGNORE TABLE tablename add UNIQUE index(col_name)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 2012-10-17
    • 1970-01-01
    相关资源
    最近更新 更多