gpt4 book ai didi

sql - #1093 表 'table' 被指定了两次,既作为 'DELETE' 的目标又作为数据的单独源

转载 作者:搜寻专家 更新时间:2023-10-30 23:26:18 28 4
gpt4 key购买 nike

在 mysql 查询中没有得到想要的结果

我已经搜索了很多但没有找到解决方案

DELETE FROM table1 WHERE username NOT IN (select t1.id from table1 as t1
inner join table1 as t2 on t1.username = t2.username and t1.id <= t2.id
group by t1.username , t1.id having count(*) <= 5 order by t1.username , t1.id desc);

输出如下:-

query should delete all rows except last 5 rows of each username

最佳答案

应该适合你

delete from
table1
where
id in ( select
id
from
( select
*,
row_number() over( partition by
username
order by
id desc) as rn
from
table1)
where
rn > 5)

关于sql - #1093 表 'table' 被指定了两次,既作为 'DELETE' 的目标又作为数据的单独源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57287543/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com