MySQL删除冗余数据--注意报错You can‘t specify target table ‘a‘ for update in FROM clause

You can’t specify target table ‘a’ for update in FROM clause意思是:
不能先select出同一表中的某些值,再update这个表(在同一语句中)

解决方案:我们就需要使用子查询,再写一次select查询嵌套筛选条件
在这里插入图片描述
这张表中,我们了解到ID=1,3是相同的数据,需要删除其中一条

错误示范

delete from name1 where id not in (select max(id) from name1  group by stu_id ,score, course)

注意,这个问题只出现于mysql,mssql和Oracle不会出现此问题。

正确示范

delete from name1 where id not in 
(
    select a.id from 
    (
        select max(id) id from name1 a where EXISTS
        (
            select 1 from name1 b  group by stu_id ,score, course HAVING count(1)>1
        )
        group by stu_id ,score, course
    ) a
)
delete from <table_name> wehere id not in (select max(id) from <table_name> group by <除id外的其他字段>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值