MySql删除重复只保留一条

本来要删除error_barcode表中barcode值重复的只保留一条

表结构如下(PS:id是自增长,该图是删除成功所以id不连续)

sql语句如下:

DELETE
FROM
    error_barcode
WHERE
    barCode IN (
        SELECT
            barCode
        FROM
            error_barcode
        GROUP BY
            barCode
        HAVING
            count(barCode) > 1
    )
AND id NOT IN (

    SELECT
        min(id)
    FROM
        error_barcode
    GROUP BY
        barCode
    HAVING
        count(barCode) > 1
)

但是却报错误

上网查找了下,这是MySql的一个问题,就是不能再同一个语句里对同一张表先select某些值在update,

那就需要把子查询在放入一个子查询中

DELETE from error_barcode
WHERE barCode in(
    select a.barCode from(select * from error_barcode)a  GROUP BY barCode having count(barCode)>1
)and id not in (
    select min(id) from(select * from error_barcode)b  GROUP BY barCode having count(barCode)>1
)

可以看到我把下面两个子查询

 SELECT
            barCode
        FROM
            error_barcode
        GROUP BY
            barCode
        HAVING
            count(barCode) > 1
 SELECT
        min(id)
    FROM
        error_barcode
    GROUP BY
        barCode
    HAVING
        count(barCode) > 1

改成

select a.barCode from(select * from error_barcode)a  GROUP BY barCode having count(barCode)>1
 select min(id) from(select * from error_barcode)b  GROUP BY barCode having count(barCode)>1

就行了

原文地址:https://www.cnblogs.com/xudongxuIT/p/8831977.html

时间: 2024-11-08 06:59:15

MySql删除重复只保留一条的相关文章

mysql 删除重复数据保留一条

验证:mysql 5.6版本 方法一: delete a from table a left join( select (id) from table group by studentName,classId) b on a.id=b.id where b.id is null; 方法二: explain delete from table where id not in (select minid from (select min(id) as minid from table group b

mysql 删除重复数据保留只保留一条

SELECT * FROM (SELECT addTime FROM motorcade.car_msg_info GROUP BY addTime HAVING COUNT(addTime) > 1) AS b) AND ID NOT IN (SELECT * FROM (SELECT MIN(ID) FROM motorcade.car_msg_info GROUP BY addTime HAVING COUNT(addTime) > 1) AS c) MIN(ID)表示保留ID最小的一条

笔记:Oracle查询重复数据并删除,只保留一条记录

1.查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 where Id in (select Id from 表 group byId having count(Id) > 1) 2.删除表中多余的重复记录,重复记录是根据单个字段(Id)来判断,只留有rowid最小的记录 DELETE from 表 WHERE (判断字段) IN ( SELECT 判断字段 FROM 表 GROUP BY 判断字段 HAVING COUNT(判断字段) > 1) A

MySQL删除重复记录只保留一条

删除表中重复记录,只保留一条: delete from 表名 where 字段ID in (select * from (select max(字段ID) from 表名 group by 重复的字段 having count(重复的字段) > 1) as b); 实例:2.当想要为某一个表建立一个唯一索引,由于表中有重复记录而无法进行时,需要删除重复记录.例表 dept id_no id_name 100 'AAA' 101 'BBB' 102 'CCC' 103 'DDD' 100 'EEE

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

用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 SELECT * FROM people WHERE peopleId IN ( SELECT peopleId FROM people GROUP BY peopleId HAVINGcount(peopleId) > 1 ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3

Mysql删除重复记录,保留id最小的一条

mysql 查询重复字段,及删除重复记录的方法MySQL, 数据库, 数据库, 字段, 服务器数据库中有个大表,需要查找其中的名字有重复的记录id,以便比较.如果仅仅是查找数据库中name不重复的字段,很容易:SELECT min(`id`),`name` FROM `table` GROUP BY `name`; 但是这样并不能得到说有重复字段的id值.(只得到了最小的一个id值)查询哪些字段是重复的也容易:SELECT `name`,count(`name`) as count FROM `

如何删除表中的重复记录只保留其中一条?

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 peopleId  

oracle 删除掉重复数据只保留一条

用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 2.删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有

【转】SQL删除重复记录,只保留其中一条

SQL:删除重复数据,只保留一条用SQL语句,删除掉重复项只保留一条在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 2.删除表中多余的重复记录,重复记录是根据单个字段(p