mysql - MySQL子查询的限制

标签 mysql sql

我需要在 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/

相关文章:

sql - 更新/删除/插入是否在 LINQ 路线图上?

php - 检索多个单项或引用的数据,无需类似操作符

mysql - 本地计算机上的 MySQL 服务启动然后停止

php - MySQL 中的动态导航菜单,PDO 显示一个实例

php - 获取按天分组的支付现金金额

php - 按子句顺序计算两列的总和 - laravel

sql - 如何在 SQL 中用 0 替换 NaN

php - 为什么即使应该更新一条记录,mysql_affected_rows 也会返回 0

c# - INSERT INTO sql语句不工作“System.Data.OleDb.OleDbException”

sql - MSSQL : Disable triggers for one INSERT