I read many posts that teach how to remove duplicate row. But in my situation, there is relationship between tables. I would like to update the relationship before deleting the duplicate rows.
I also read a question in 2013 that is very similar to my situation but I didn't archive it using the method in the posts. deleting duplicates in sql and modifying relationship table accordingly
I am new to mysql and need some help. Thank you in advance. I build this with laravel 8 and mysql 8 and I manage the tables in phpmyadmin.
Here is my tables:
actors
id       actor_id
1        fghj
2        fghj        (to be removed)
3        fghj        (to be removed)
4        zxcv
5        zxcv        (to be removed)
videos
id       code
1        aaa
2        bbb
3        ccc
actor_video
actor_id()     video_id
(=actors.id)   (=videos.id)
1            1
1            3        (to be modified to 1 1)
2            4
2            5        (to be modified to 2 4)
3            2        (to be modified to 3 1)
3            3        (to be modified to 3 1)
I have an idea but I failed to code it. My plan is:
- create a column in actors correct_id
- find all duplicate rows in actors with COUNT(actor_id)
- assign correct_id = min(id)in each duplicate group
- modify the relationship table by replacing actor_video.actor_idtoactors.correct_id
- remove duplicate rows in actor_video
- remove duplicate rows in actors
Step 1:
ALTER TABLE actors
ADD correct_id varchar(255);
Step2:
SELECT actor_id,COUNT(actor_id)
FROM `actors` 
GROUP BY actor_id
HAVING COUNT(actor_id) >1
Step3:
????
(this is wrong code. Just try to express my idea)
UPDATE actors
SET correct_id = min(id)
WHERE COUNT(actor_id) >1
Then I stuck since step 3. Sorry if this a slow and stupid idea. Please correct me for a efficient way. Thank you
 
     
    