TransWikia.com

sql, delete duplicate records using an ID

Stack Overflow Asked by user12929912 on December 18, 2021

So if the DOCUMENTSenter image description here table if employeeId 1 for example has 5 files with the same filename then delete the 4 just remain 1 file(remove the duplicate or with the same filename) and the same goes to all.

I am using mariadb

#My query to get all duplicate records

#Code

SELECT id, employeeId, filename, COUNT(filename) FROM DOCUMENTS GROUP BY filename
      HAVING COUNT(filename) > 1;

2 Answers

if employeeId 1 for example has 5 files with the same filename then delete the 4 just remain 1 file

One approach uses a delete statement with a self-join:

delete d1
from documents d1
inner join documents d2 
    on d2.id < d1.id 
    and d2.filename = d1.filename
    and d2.employeeid = d1.employeeid

This removes duplicates on (employeeid, filename) while retaining the row with the smallest id.

You should be able to work around the safe mode error in MariaDB by moving one condition to the where clause, eg the filter on id;

delete d1 from documents d1 inner join documents d2 on d2.filename = d1.filename and d2.employeeid = d1.employeeid where d2.id < d1.id

Answered by GMB on December 18, 2021

I am not sure if you're trying to select only distinct items or find out those that have duplicates. In case you only want to select distinct / unique values :

 SELECT distinct id, employeeId, filename FROM DOCUMENTS GROUP BY filename

I'd suggest you to move the selected unique records into a temp table and then delete the original table records and then insert the unique records from temp table back to original .

Answered by wonderboy13 on December 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP