V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
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
FelixXie
V2EX  ›  MySQL

mysql 相邻的相同数据如何去重

  •  
  •   FelixXie · 2017-05-25 11:38:17 +08:00 · 4249 次点击
    这是一个创建于 2521 天前的主题,其中的信息可能已经有所发展或是发生改变。

    例如:

    • 1
    • 1
    • 2
    • 1
    • 1

    五条数据

    期望结果:

    • 1
    • 2
    • 1
    12 条回复    2017-05-28 08:51:55 +08:00
    lxh1217lzz
        1
    lxh1217lzz  
       2017-05-25 11:42:38 +08:00   ❤️ 1
    数据库储存本来就是无序的,哪来相领一说啊。。
    shoaly
        2
    shoaly  
       2017-05-25 12:23:23 +08:00   ❤️ 1
    方式错了, 应该是在插入之前 就判断好是否需要插入.
    woshixiaohao1982
        3
    woshixiaohao1982  
       2017-05-25 12:43:20 +08:00   ❤️ 1
    @shoaly 人家想说的是 以前就有的数据,,但是现在要去重
    johnny23
        4
    johnny23  
       2017-05-25 12:47:08 +08:00 via iPhone   ❤️ 1
    方法很多...建一张一摸一样的表 只是把重复的那个字段设为唯一值 然后写个程序导入进去 抓住异常就跳过 不过效率可能很低 或者用过 excel 来处理...看你的数据量咯 太大还是用点数据库自带的一些方式来处理比较好
    reus
        5
    reus  
       2017-05-25 12:52:55 +08:00   ❤️ 1
    delete from t where id in (select id from t a left join t b on a.id = b.id + 1 and a.n = b.n)
    jhdxr
        6
    jhdxr  
       2017-05-25 12:53:19 +08:00   ❤️ 2
    因为 MySQL 没有 rownum,但你的表总有 id 吧,如果它是自增主键(不要求连续,但能够用它来判断你所谓的顺序),那么
    ```sql
    SELECT
    v
    FROM
    a AS table1
    WHERE
    table1.v != (
    SELECT
    table2.v
    FROM
    a AS table2
    WHERE
    table1.id < table2.id
    ORDER BY
    table2.id
    LIMIT 1
    )
    OR NOT EXISTS (
    SELECT
    *
    FROM
    a AS table3
    WHERE
    table1.id < table3.id
    )
    ```


    当然这么做我真的觉得有点蛋疼。。。
    reus
        7
    reus  
       2017-05-25 12:53:33 +08:00   ❤️ 1
    select * from t where id not in (select id from t a left join t b on a.id = b.id + 1 and a.n = b.n)
    reus
        8
    reus  
       2017-05-25 12:54:29 +08:00
    select * from t where id not in (select id from t a left join t b on a.id = b.id + 1 and a.n = b.n order by id asc) 不过只能处理 id 递增的情况,不能有洞
    FelixXie
        9
    FelixXie  
    OP
       2017-05-25 12:59:39 +08:00
    @shoaly 嗯,确实是。
    debuggerx
        10
    debuggerx  
       2017-05-25 13:10:27 +08:00   ❤️ 1
    表名:test
    id value
    1 1
    2 1
    3 1
    4 2
    5 2
    6 3
    7 4
    8 4
    9 5

    select id ,value from test t where t.value <> (select value from test where id = t.id -1) or t.id = 1 ;

    结果:
    1 1
    4 2
    6 3
    7 4
    9 5

    随手写的不知道到是不是这个。。
    复杂的我肯定就直接写 py 了……
    CRVV
        11
    CRVV  
       2017-05-25 13:25:57 +08:00   ❤️ 1
    受 jhdxr 的启发,用 PostgreSQL 可以写成这样,其实没什么区别,只是短一些

    SELECT a.id, a.value FROM a
    LEFT JOIN LATERAL (SELECT * FROM a AS next WHERE next.id > a.id LIMIT 1) AS next ON TRUE
    WHERE a.value = next.value IS NOT TRUE;
    mingyun
        12
    mingyun  
       2017-05-28 08:51:55 +08:00
    10 楼可行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2793 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:00 · PVG 22:00 · LAX 07:00 · JFK 10:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.