gpt4 book ai didi

mysql - MySQL子查询的限制

转载 作者:行者123 更新时间:2023-11-30 00:36:21 25 4
gpt4 key购买 nike

我需要在 MySQL 中进行 SQL 查询,查询应从两个表 store 和 item 中选择,store 有一个或多个项目,查询应从这两个表中选择,按 store 表的创建日期和更新日期排序item表,每个商店的最大商品数是10个,意味着如果商店有超过10个商品,每个商店的商品不超过10个,我考虑做这个查询:

select * 
from store s, item i
where s.store_id = i.store_id
and i.item_id in (select i1.item_id
from item i1
where i1.store_id = s.store_id
limit 10)
group by s.store_id, i.item_id
order by s.created_at, i.updated_at

但我收到一个错误

This version of MySQL doesn't yet support limit in subquery

有什么办法可以做到这一点吗?

最佳答案

您可能需要升级 MySQL 版本。

如果不确定这是否有效,但值得一试

select s.*,i.* 
from store s
JOIN item i on s.store_id=i.store_id
JOIN (select i1.item_id from item i1 where i1.store_id = s.store_id limit 10)
itemids ON i.item_id=itemids.item_id
group by s.store_id, i.item_id
order by s.created_at, i.updated_at

我在子选择上进行连接,然后在 i.item_id 上连接它们

关于mysql - MySQL子查询的限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22144751/

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