该表是Bom表,数据量很大,从Excel导入的。当时由于时间关系,没有在Excel中去重,就导入了。
之前没有意识到标识字段的重要性,所以在建表时没有设该字段。
今天发现有重复数据,于是就想办法去重。
表字段,bitem(商品),item(物料),qty,lvl 。
修改表结构
alter table t1 add col1 int default 0 not null
给现有col1标识
set @rown:=0
update t1 set col1=(@rown:=@rown+1) where col1=0
现在有唯一标识(id)了,可以去重
delete from t1 where col1 in
( select a.col1 from (
select max(col1) as col1 from t1 group by bitem,item having count(*) >1
) as a
);
删除重复数据成功
是不是复杂点,为啥不用
delete from t1 where col1 in
(select max(col1) as col1 from t1 group by bitem,item having count(*) >1 );
这个语句在MSSQL中是对的,在MySql5.7中会报错
因为会碰到You can’t specify target table for update in FROM clause错误,就是在更新的时候筛选子集和目标表不能一样。
同样情况会发生在update 用法中.
对于无标识id而要显示行号,也可以用如下命令
select (@row:=@row+1) as no , a.* from t1 a, (select @row:=0), t;