在数据库中查找重复的列多个值
Find duplicate column multiple value in 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
如果 home
、path
和 name
相等,但前提是所有 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"
如果您确实想删除 重复项,则使用存在逻辑:
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);
您可以按 home
、path
、name
和 select MAX(id)
.
列分组
在这种情况下,对于 3 列的每个组合,SQLite 将 return 只有 1 行,即最大 id
:
SELECT MAX(id) id, home, path, name, layer
FROM tablename
GROUP BY home, path, name
ORDER BY id
这是一个documented feature of SQLite.
如果要删除重复项:
DELETE FROM tablename
WHERE id NOT IN (SELECT MAX(id) FROM tablename GROUP BY home, path, name)
参见demo。
结果:
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
我有一个包含以下列的 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
如果 home
、path
和 name
相等,但前提是所有 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"
如果您确实想删除 重复项,则使用存在逻辑:
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);
您可以按 home
、path
、name
和 select MAX(id)
.
列分组
在这种情况下,对于 3 列的每个组合,SQLite 将 return 只有 1 行,即最大 id
:
SELECT MAX(id) id, home, path, name, layer
FROM tablename
GROUP BY home, path, name
ORDER BY id
这是一个documented feature of SQLite.
如果要删除重复项:
DELETE FROM tablename
WHERE id NOT IN (SELECT MAX(id) FROM tablename GROUP BY home, path, name)
参见demo。
结果:
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 |