Remove Duplicates from a database with no primary key in Mysql
- We can create a customized Row_Number and Map it to a Partition in table as follows
With cte as (SELECT id,studentName,streetAddress, ROW_NUMBER() OVER(PARTITION BY id,studentName,streetAddress order by id,studentName,streetAddress) AS row_num
FROM springjdbc.student)
select * from cte WHERE row_num>1;
Delete from cte WHERE row_num>1;
- Where our table is as follows
SELECT id, studentName, streetAddress
FROM springjdbc.student; - To use the above query in database abstraction layers in code we can use as follows.
Delete from student where id in (Select ds.id from (SELECT id,studentName,streetAddress, ROW_NUMBER() OVER(PARTITION BY id,studentName,streetAddress order by id,studentName,streetAddress) AS row_num FROM springjdbc.student) ds where ds.row_num>1)
c
Comments
Post a Comment