V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
uti6770werty
V2EX  ›  MySQL

求助,指定字段相同的数据,删除重复,保留 id 最大值的一条

  •  
  •   uti6770werty · Nov 23, 2019 · 4906 views
    This topic created in 2356 days ago, the information mentioned may be changed or developed.

    有一张表,表名:machinetbl,字段名用了中文,有如下字段:

    id|型号|生产时间|序列号|SPF|重量|大小|InterID|UpdateTime

    请问:
    (型号,生产时间,InterID,序列号,)这 4 个字段相同的数据,只保留 1 条 id 最大值的记录,重复的数据原表原地删除

    这条 MySQL 语句怎么写?

    mysql> delete from machinetbl where id in(
    select id from machinetbl t where exists(select 1 from machinetbl t2 where t2.型号=t.型号 and t2.生产时间=t.生产时间 and t2.InterID=t.InterID and t2.序列号=t.序列号 and t2.id<t.id))
    1093 - You can't specify target table 'machinetbl' for update in FROM clause
    

    折腾了一晚,还是不成,求助

    8 replies    2019-11-23 14:26:59 +08:00
    k9990009
        1
    k9990009  
       Nov 23, 2019 via Android
    云一下 not in 分组求最大
    uti6770werty
        2
    uti6770werty  
    OP
       Nov 23, 2019
    @k9990009 没能明白您意思啊,我今晚看了 N 多例子,其实我也知道网上例子很多的,百度真的很多,但都是根据 1~2 个字段的来判断,像是我这个情况需要 4 个字段做判断的,我也是照葫芦画瓢地改,work 不了,非常头疼,只能现在去睡觉了,明天等直接可用的答案,慢慢的再学习理解。
    VEEX6
        3
    VEEX6  
       Nov 23, 2019
    select max(id) as id from machinetbl group by 型号,生产时间,InterID,序列号 Having Count(*) > 1
    取到最大 id 和记录,按条件删除完事
    luanjia
        4
    luanjia  
       Nov 23, 2019
    1. 根据唯一性确定多列筛选条件,筛选多余数据:

    SELECT 型号,生产时间,InterID,序列号
    FROM 表
    GROUP BY 型号,生产时间,InterID,序列号
    HAVING count(1) > 1;


    2. 留下 id 最大的数据,选择出重复列待删除数据:
    SELECT id as id
    FROM 表 a
    WHERE (a.型号,a.生产时间,a.InterID,a.序列号)
    in
    (SELECT 型号,生产时间,InterID,序列号
    FROM t_action_applink_lang
    GROUP BY 型号,生产时间,InterID,序列号
    HAVING count(1) > 1)
    and id not in (SELECT max(id)
    FROM 表
    GROUP BY 型号,生产时间,InterID,序列号
    HAVING count(1) > 1)

    3. 如果直接从上面选择语句删除重复数据,则会报错.所以给表一个别名
    DELETE
    FROM t_action_applink_lang
    WHERE id in (SELECT id as id
    FROM 表 a
    WHERE (a.型号,a.生产时间,a.InterID,a.序列号)
    in
    (SELECT 型号,生产时间,InterID,序列号
    FROM t_action_applink_lang
    GROUP BY 型号,生产时间,InterID,序列号
    HAVING count(1) > 1)
    and id not in (SELECT max(id)
    FROM 表
    GROUP BY 型号,生产时间,InterID,序列号
    HAVING count(1) > 1)
    )
    luanjia
        5
    luanjia  
       Nov 23, 2019
    楼中回复好像不能使用 markdown,格式不对的话自己可以复制出去看一下哈:
    最终 sql 是:
    DELETE
    from 表
    WHERE id in (SELECT id as id
    FROM 表 a
    WHERE (a.型号,a.生产时间,a.InterID,a.序列号)
    in
    (SELECT 型号,生产时间,InterID,序列号
    FROM t_action_applink_lang
    GROUP BY 型号,生产时间,InterID,序列号
    HAVING count(1) > 1)
    and id not in (SELECT max(id)
    FROM 表
    GROUP BY 型号,生产时间,InterID,序列号
    HAVING count(1) > 1)) as p);
    uti6770werty
        6
    uti6770werty  
    OP
       Nov 23, 2019
    @luanjia 感谢感谢,水平有限,一时间理解不了,不过会加紧学习理解。。。

    5 楼的答案,似乎是有些格式不对,但琢磨不出 p 附近的括号的问题在哪里。。。。,报错如下:

    1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as p)' at line 14
    mysql>
    wysnylc
        7
    wysnylc  
       Nov 23, 2019
    java8 stream,一行代码搞定
    luanjia
        8
    luanjia  
       Nov 23, 2019
    @uti6770werty #6 我之前写过这个 sql,但是表明列名跟你的不一样,所以我在回复中替换你的列名使出现错误;你可以从内层 select 复制出来逐步运行一下。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5709 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 77ms · UTC 08:44 · PVG 16:44 · LAX 01:44 · JFK 04:44
    ♥ Do have faith in what you're doing.