在db中查找重复的列多个值

问题描述

我有一个包含以下列的 sqlite 数据库

id   home   path    name    layer
1   /home   test    user1   1
2   /etc    test2   user2   1
3   /home   test    user3   1
4   /home   test    user4   1
5   /home   test    user1   1
6   /etc    test2   user2   1

如果 homepathname 相等,但仅当所有 3 个都相同时,我如何删除所有重复项?

sl id 1 应该被删除,因为它是 id 5 的副本,而 id 2 应该删除,因为它是 id 6 的副本。

它应该是这样的:

id   home   path    name    layer
3   /home   test    user3   1
4   /home   test    user4   1
5   /home   test    user1   1
6   /etc    test2   user2   1

解决方法

您可以通过 group by 实现这一目标:

select
  *
from data

group by "home","path","name"
having max("id")

order by "id"

sqlfiddle

,

如果您确实想删除重复项,请使用存在逻辑:

DELETE
FROM yourTable
WHERE EXISTS (SELECT 1 FROM yourTable t2
              WHERE t2.home = yourTable.home AND
                    t2.path = yourTable.path AND
                    t2.name = yourTable.name AND
                    t2.id > yourTable.id);

如果您只想以这种方式查看您的数据,请尝试:

SELECT t1.*
FROM yourTable t1
WHERE NOT EXISTS (SELECT 1 FROM yourTable t2
                  WHERE t2.home = t1.home AND t2.path = t1.path AND
                        t2.name = t1.name AND t2.id > t1.id);
,

您可以按列 homepathname 进行分组,然后选择 MAX(id)
在这种情况下,SQLite 将为 3 列的每个组合仅返回 1 行,即最大 id 行:

SELECT MAX(id) id,home,path,name,layer
FROM tablename
GROUP BY home,name
ORDER BY id

这是documented feature of SQLite

如果要删除重复项:

DELETE FROM tablename
WHERE id NOT IN (SELECT MAX(id) FROM tablename GROUP BY home,name)

参见demo
结果:

id home 路径 名称
3 /home 测试 user3 1
4 /home 测试 user4 1
5 /home 测试 user1 1
6 /etc test2 user2 1