一条SQL语句删除重复的记录

去面试时,被问到用一条SQL语句删除重复的记录,当时做不出来,回头想想,方法如下:

新增一个表,用于测试

create table table1 (id int primary key, name char(20) );

添加样品记录

insert into table1 values (1,'hello') , (2,'world'), (3,'hello'), (4,'hello'), (5,'world'), (6,'python');

删除name相同的记录(保留第一条)

delete t2 from table1 t1, table1 t2 where t1.name=t2.name and t1.id<t2.id;

猜你喜欢

转载自ipython.iteye.com/blog/1423771