Delete duplicate rows

Set Safe Update to null

We need to set SQL SAFE updates to 0 before using this syntax. If not, error 1175 will occur

SET SQL_SAFE_UPDATES = 0;

See multiple rows

SELECT 
    contractAddress, 
    count(contractAddress) as counter 
FROM
    production.contract 
GROUP BY
    contractAddress HAVING count(contractAddress) > 1 
ORDER BY 
    counter DESC

Delete multiple rows

DELETE FROM 
	production.contract
WHERE 
id not in
	(SELECT id FROM (
			SELECT MAX(id) as id
			FROM production.contract
			GROUP BY contractAddress
			) 
	AS c)

WHERE id not in

is the condition query to delete datas WHERE ID is not in (SELECTED IDs)

SELECT MAX ID and GROUP BY contractAddress

returns IDs with the most updated row DISTINCT (GROUP BY) contractAddress

RESULT

Check deleted

SELECT 
    contractAddress, 
    count(contractAddress) as counter 
FROM
    production.contract 
GROUP BY
    contractAddress HAVING count(contractAddress) > 1 
ORDER BY 
    counter DESC

Set Safe Update to 1

SET SQL_SAFE_UPDATES = 1;

Last updated

Was this helpful?