Find Duplicate entries :
SELECT Count(*) as cnt
,
col1, col2
FROM
table_name GROUP BY
col1, col2
HAVING
Count(*)
> 1
Remove Duplicate entries:
Approach 1:
DELETE FROM table_name
USING table_name, table_name AS t1
WHERE
(table_name.id > t1.id)
AND (table_name.
col1
=
t1
.
col1
)
AND (table_name.
col2
=
t1
.
col2
)
Note: Here id is identity column (Auto increment column)
and
Replace
col1, col2
and table_name
Approach 2:
DELETE t3
FROM (
SELECT t1.col1, t1.id
FROM (
SELECT col1
FROM table_name
GROUP BY col1
HAVING COUNT(col1) > 1
) AS t0 INNER JOIN
table_name
t1 ON t0.name = t1.name) AS t2 INNER JOIN
table_name
t3 ON t3.name = t2.name
WHERE t2.id < t3.id;
No comments:
Post a Comment