Deleting duplicates of tables

那年仲夏 提交于 2019-12-12 13:29:55

问题


In my activity_logs, it contains columns: material_name, user_id, mod_result (this marks a test if Pass/Fail), cert_links. Somehow the users generate twice the entry of the material_name with the cert_links column left blank.

I can list duplicates to all user_id with:

SELECT user_id, material_name, mod_score, cert_links, start_time 
FROM activity_logs 
WHERE mod_result = 'Pass' AND cert_links = ''

I want to delete the duplicate entries that has mod_result = 'Fail' and cert_links = ''


回答1:


you can use:

Delete from activity_logs WHERE mod_result = 'Fail' AND cert_links = '' or ''NULL'



回答2:


3 steps:

Step 1: Move the non duplicates (unique tuples) into a temporary table

CREATE TABLE new_table AS SELECT * FROM old_table WHERE 1 GROUP BY [COLUMN TO remove duplicates BY];

Step 2: delete the old table

DROP TABLE old_table;

Step 3: rename the new_table to the name of the old_table

RENAME TABLE new_table TO old_table;



来源:https://stackoverflow.com/questions/22004725/deleting-duplicates-of-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!