分享java带来的快乐

我喜欢java新东西

删除部分重复字段的记录

创建临时表
create table tmp_dur as
select a.shop_id,a.city_id,a.product_id,max(a.id) as mid from 51fanli_tuandh_item as a group by a.shop_id,a.city_id,a.product_id having count(*) > 1

查询联合表
SELECT a.shop_id,a.city_id,a.product_id from tmp_dur a , 51fanli_tuandh_item b where a.shop_id = b.shop_id and a.city_id = b.city_id and a.product_id = b.product_id

删除重复项目
delete from 51fanli_tuandh_item
where 51fanli_tuandh_item.id != (
SELECT b.mid from tmp_dur b
where 51fanli_tuandh_item.city_id = b.city_id and 51fanli_tuandh_item.shop_id = b.shop_id and 51fanli_tuandh_item.product_id = b.product_id
);

删除临时表
drop tmp_dur;

添加唯一
ALTER TABLE  `51fanli_tuandh_item` ADD UNIQUE (
`shop_id` ,
`city_id` ,
`product_id`
);

posted on 2011-08-29 17:32 强强 阅读(350) 评论(0)  编辑  收藏 所属分类: mysql


只有注册用户登录后才能发表评论。


网站导航: