鉴于下表:
+----+--------+--------+------------+
| id | val1 | val2 | timestamp |
+----+--------+--------+------------+
| 1 | 50 | 100 | 1471869500 |
| 2 | 50 | 100 | 1471869800 |
| 3 | 60 | 70 | 1471864400 |
| 4 | 60 | 80 | 1471863300 |
| 5 | 60 | 90 | 1471862200 |
| 6 | 60 | 100 | 1471861100 |
+----+--------+--------+------------+
我想删除列val1和val2中具有重复值的所有行(因此这将是行1和2)但保留最后插入的行(第2行)。 在示例表中,只会删除第1行。
我能为我创建一个SQLite语句吗?
我试过了:
Select statement to find duplicates on certain fields
Delete all Duplicate Rows except for One in MySQL?
https://dba.stackexchange.com/questions/101697/sqlite3-remove-duplicates-by-multiple-columns
干杯
答案 0 :(得分:1)
似乎SQLite不支持直接加入其DELETE
语法,但您仍然可以使用相关子查询来完成工作。试试这个问题:
DELETE
FROM yourTable
WHERE timestamp t <> (SELECT MAX(p.timestamp) FROM yourTable p
WHERE p.val1 = t.val1 AND p.val2 = t.val2)