首先是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子句中限制,但在子子查询中允许它,例如添加“select * from”外面原来“选择食物......”可以解决限制问题。说真的,我不知道为什么,但我测试一下,没关系。答案 0 :(得分:2)
你应该在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