删除没有主键的重复mysql行

时间:2022-03-24 04:40:00

Hi i have a mysql table without primary key and i need to delete the duplicated rows. how can i do so?

嗨,我有一个没有主键的mysql表,我需要删除重复的行。我怎么能这样做?

user_id category_id
1            2
1            3
1            4
1            2
2            2
2            3
2            2

2 个解决方案

#1


17  

 CREATE TABLE temp SELECT DISTINCT * FROM tablename;
 ALTER TABLE tablename RENAME junk;
 ALTER TABLE temp RENAME tablename;

#2


2  

Since you cannot differentiate 2 identical rows, you cannot delete just one of them. The way you need to think about it is like this:

由于无法区分2个相同的行,因此无法仅删除其中一个行。你需要考虑它的方式是这样的:

insert into new_better_table
select user_id, category_id from old_table group by user_id, category_id

#1


17  

 CREATE TABLE temp SELECT DISTINCT * FROM tablename;
 ALTER TABLE tablename RENAME junk;
 ALTER TABLE temp RENAME tablename;

#2


2  

Since you cannot differentiate 2 identical rows, you cannot delete just one of them. The way you need to think about it is like this:

由于无法区分2个相同的行,因此无法仅删除其中一个行。你需要考虑它的方式是这样的:

insert into new_better_table
select user_id, category_id from old_table group by user_id, category_id