0

I need to add this contrain to my table called 'kraken'

ALTER TABLE kraken ADD UNIQUE unique_index(exchange_pair,timestamp);

this shows me that i have duplicated entries on those 2 columns (exchange_pair and timestamp) so first i have to delete all the duplicated entries based on those 2 columns

this is what i found but im not sure if my query would work since i need to delete only duplicated entris with column 'exchange_pair' and 'timestamp', i dont want to mess things up with my table https://www.mysqltutorial.org/mysql-delete-duplicate-rows/

1
  • 1
    My advise is that you create a replica of table and do the test in there, with query like "CREATE TABLE temp_table AS SELECT * FROM original_table" and perform operations over temp_table, also you can use the temp_table as backup. Commented Feb 22, 2021 at 18:54

1 Answer 1

0

The easiest way to remove duplicates is to join the table with itself:

DELETE z.* 
  FROM kraken AS w
  JOIN kraken AS z ON z.exchange_pair = w.exchange_pair
                  AND z.timestamp = w.timestamp
                  AND z.id > w.id -- or what primary key you have
;

All duplicates will be deleted except one with the lowest id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.