从sql中删除重复数据

时间:2018-04-20 09:25:39

标签: mysql sql mysqli duplicates

我有一个名为“data”的sql数据库和一个表“disk”,其中有5列

CREATE TABLE disk(   id int(11)NOT NULL,   title text COLLATE utf8_unicode_ci NOT NULL,   link text COLLATE utf8_unicode_ci NOT NULL,   mag text COLLATE utf8_unicode_ci NOT NULL,   size varchar(10)COLLATE utf8_unicode_ci NOT NULL )ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;

“mag”列有一些副本。

我想删除mag列相同的完整行。

注意: - 假设mag列有1,2,3,4,4,5 ....我想从中删除一个重复的4。意味着我不想完全删除4.一个“4”必须保留。

告诉SQL查询可以做到这一点......在将这个问题标记为重复之前,我必须告诉你,我已经看到了类似的问题,但没有一个有效。欲了解更多信息,请发表评论。

5 个答案:

答案 0 :(得分:6)

尝试以下操作删除具有相同d列的副本,并保留一行具有最低id值:

DELETE d1 
FROM  disk d1, disk d2 
WHERE d1.id > d2.id AND 
      d1.d = d2.d;

答案 1 :(得分:1)

试试这个:

DELETE disk
FROM disk
INNER JOIN (
       SELECT id,
               d,
               CASE WHEN d = @prevd 
                       THEN @id:=@id+1
                    ELSE @id:=1
               END AS rankNum,
               @prevd:=d AS prd
       FROM disk, (SELECT @prevd:=NULL,@id:=NULL) t
      ) t1
ON disk.id = t1.id
WHERE rankNum >= 2;

演示请按照以下链接:

  

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=318e94a135853fcd15b14e4b8bbf1fdc

答案 2 :(得分:0)

delete from disk
where id  in 
(
select id 
from task
group by id 
having count(id) >1
)

答案 3 :(得分:0)

您可以执行以下操作..
创建新表并保留随机行:

  1. 首先将表disk(唯一数据)复制到临时表disk2

  2. 删除表disk

  3. 将临时表disk2重命名为disk

    create table disk2 select * from disk group by d;
    
    drop table disk;
    
    rename table disk2 to disk;
    
  4. 注意:我们将group by*一起使用,因为OP并不关心要保留哪一行。


    创建新表并保持行的最小或最大ID: 在使用minmax id

    保持行的同时执行此操作的另一种方法
    /*copy data from disk to temp table disk2*/
    create table disk2 select * from disk
        where id in (select min(id) from disk group by d);
    /*drop table disk*/
    drop table disk;
    /*rename temp table to disk*/
    rename table disk2 to disk;
    


    更新:另一种方法
    从现有表中删除重复项

        /*first create a dups table for duplicates*/
        create table dups select * from disk
            where id not in (select min(id) from disk group by d);
        /*now delete all rows which are present in dups table*/
        delete from disk where id in (select id from dups);
        /*now delete the dups table*/
        drop table dups;
    

答案 4 :(得分:0)

无需创建任何临时表

我希望这对你有用

DELETE ColumnName
FROM TableName
INNER JOIN 
(
    SELECT  MAX(ID) AS ID
    FROM TableName
    GROUP BY ID
    HAVING COUNT(*) > 1
) Duplicate on Duplicate.ID = TableName.ID
WHERE TableName.ID < Duplicate.lastId;

另请查看以下链接以获取更多建议

MySQL delete duplicate records but keep latest