sql,使用一个ID删除重复记录
sql, delete duplicate records using an ID
因此,如果文档 table 如果 employeeId 1 有 5 个具有相同文件名的文件,则删除 4 个只剩下 1 个文件(删除重复的或具有相同文件名的文件)和所有人都一样。
我正在使用 mariadb
#我的查询获取所有重复记录
#代码
SELECT id, employeeId, filename, COUNT(filename) FROM DOCUMENTS GROUP BY filename
HAVING COUNT(filename) > 1;
我不确定您是要 select 仅尝试不同的项目还是找出重复的项目。
如果您只想 select 不同的/唯一的值:
SELECT distinct id, employeeId, filename FROM DOCUMENTS GROUP BY filename
我建议您将 selected 的唯一记录移动到临时 table 中,然后删除原始 table 记录,然后从临时 [=] 中插入唯一记录17=]回到原来的.
if employeeId 1 for example has 5 files with the same filename then delete the 4 just remain 1 file
一种方法使用带有自连接的 delete
语句:
delete d1
from documents d1
inner join documents d2
on d2.id < d1.id
and d2.filename = d1.filename
and d2.employeeid = d1.employeeid
这将删除 (employeeid, filename)
上的重复项,同时保留具有最小 id
的行。
您应该能够通过将一个条件移动到 where
子句来解决 MariaDB 中的安全模式错误,例如 id
;
上的过滤器
删除d1
来自文件 d1
内部连接文档 d2
在 d2.filename = d1.filename
d2.employeeid = d1.employeeid
其中 d2.id < d1.id
因此,如果文档
我正在使用 mariadb
#我的查询获取所有重复记录
#代码
SELECT id, employeeId, filename, COUNT(filename) FROM DOCUMENTS GROUP BY filename
HAVING COUNT(filename) > 1;
我不确定您是要 select 仅尝试不同的项目还是找出重复的项目。 如果您只想 select 不同的/唯一的值:
SELECT distinct id, employeeId, filename FROM DOCUMENTS GROUP BY filename
我建议您将 selected 的唯一记录移动到临时 table 中,然后删除原始 table 记录,然后从临时 [=] 中插入唯一记录17=]回到原来的.
if employeeId 1 for example has 5 files with the same filename then delete the 4 just remain 1 file
一种方法使用带有自连接的 delete
语句:
delete d1
from documents d1
inner join documents d2
on d2.id < d1.id
and d2.filename = d1.filename
and d2.employeeid = d1.employeeid
这将删除 (employeeid, filename)
上的重复项,同时保留具有最小 id
的行。
您应该能够通过将一个条件移动到 where
子句来解决 MariaDB 中的安全模式错误,例如 id
;
删除d1 来自文件 d1 内部连接文档 d2 在 d2.filename = d1.filename d2.employeeid = d1.employeeid 其中 d2.id < d1.id