mysql删除重复数据,并保留一条

因为系统bug在对所在店铺的会员进行屏蔽的时候没有进行查重操作,导致在屏蔽表中出现了重复的数据,所以需要删除此表中姓名重复的数据,并保留其中的一条。模拟数据如下图:
模拟数据
第一条sql :

delete from t_black_list where sellerId = 120055122 and names in (
  select names from t_black_list where sellerId = 120055122 and types = 4 group by names having count(names) > 1
)
and id not in (
  select id from t_black_list where sellerId = 120055122 and types = 4 group by names having count(names) > 1
)

遇到了问题:

遇到You can’t specify target table ‘表名’ for update in FROM clause这样的错误,它的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中),即不能依据某字段值做判断再来更新某字段的值。

解决:(中间表过度)

 select * from t_black_list where sellerId = 120055122  and names in (
  SELECT a.names from (select names from t_black_list where sellerId = 120055122  and types = 4 group by names having count(names) > 1) a
)
and id not in (
  SELECT b.id  from (select id from t_black_list where sellerId = 120055122  and types = 4 group by names having count(names) > 1) b
)

注:
mysql group by 会选择id最小的那个

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值