首先是 SQL =
delete
from eat_history
where food in
( select food
from eat_history
where name ='minicake'
and cream='yes'
order
by eat_time desc
limit 1
) t1
我的目的是删除最近吃过的名为“minicake”和cream=“yes”的食物(可能有多个项目)。正如我在标题中指出的那样,MySQL 不允许这样做。然而,就我而言,“限制1”是关键点。有没有好的解决方法,最好是单个 SQL 语句?
=====
编辑:我在另一个问题中找到了答案,虽然MySQL不允许在IN子句中进行限制,但在子子查询中允许,例如在原来的“选择食物...”之外添加“选择*来自”可以解决该限制。说真的,我不知道为什么,但我测试了一下,没问题。
最佳答案
首先您应该删除 in 子句后的 t1
delete from eat_history where food in (
select food from eat_history where name='minicake' and cream='yes'
order by eat_time DESC limit 1
)
但是您可以通过连接使用组来避免限制
delete from eat_history e
inner join (
select food, max(eat_time) max_time
from eat_history
where name='minicake' and cream='yes'
group by foo
) t on t.food = e.food and t.max_time = e.eat_time
关于mysql - MySQL 中不允许子查询限制,但我需要 "it",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50393848/