我有一个表listed_users有两个TEXT列,code并且username:
code | username
-----|---------
aa | john_doe
ab | jane_doe
ca | john_doe
ca | john_doe <-- duplicate
da | ryan_doe
Run Code Online (Sandbox Code Playgroud)
我想编写一条命令来删除重复的命令,例如ca | john_doe,其中相同的信息会出现在多行的两列中。
To delete one of a pair of duplicate rows, you must have some mechanism to identify it. In SQLite, this would be the rowid.
The following query returns the rowid values of all the rows you want to keep, i.e., one row for each unique code/name combination:
SELECT min(rowid)
FROM listed_users
GROUP BY code, username;
Run Code Online (Sandbox Code Playgroud)
You want to delete all rows not in that list:
DELETE FROM listed_users
WHERE rowid NOT IN (SELECT min(rowid)
FROM listed_users
GROUP BY code, username);
Run Code Online (Sandbox Code Playgroud)