如果mysql数据库A表没有主键字段,当表中有重复数据时,如何使用一条sql删除多余的重复数据。
1.找出不重复数据
select * from test group by a, b
# 或
select distinct * from test
2.将不重复数据插入到同结构的临时表
create table tmp select distinct * from test
3.删除重复数据的两种方案
#方案一
#删除原表,并重命名临时表
drop table test
alter table tmp rename to test
#方案二
#清空原表,临时表写入原表,再删除临时表
delete from test
insert into test slect * from tmp
drop table tmp
4.使用存储过程实现方案一
#使用存储过程完成
# 创建存储过程
create procedure p()
begin
create table temp select distinct * from test;
drop table test;
alter table emp rename to test;
end
# 调用存储过程
call p();