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
2017-05-25 12:53:19 +08:00
因为 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
2017-05-25 12:53:33 +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)
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 递增的情况,不能有洞
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
2017-05-25 13:25:57 +08:00
受 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
2017-05-28 08:51:55 +08:00
10 楼可行
第 1 页 / 共 1 页
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。