表里的某列有重复数据,如下:
执行以下sql:
delete from t_netbar_conf_item_real where
item_key in (select item_key from (select item_key from t_netbar_conf_item_real group by item_key having count(item_key) > 1) as s1)
and id not in (select id from (select min(id) as id from t_netbar_conf_item_real group by item_key having count(item_key)>1) as s2);
结果,重复数据没了:
补充:
1. 查询去重如下:(即查询时把重复数据过滤掉)
select * from t_netbar_conf_item_real where id in (select Max(id) from t_netbar_conf_item_real group by item_key)
2. 给item_key添加唯一约束:
ALTER TABLE `t_netbar_conf_item_real` ADD unique(`item_key`);