我有一个查询从表中选择产品:
Select * FROM products WHERE category_id in (1,2,3,4,5) LIMIT 100
我想对每个类别返回的条目数量加以限制,而不是所有类别的总和。
我拥有它的方式有时它只能返回一个类别的100个产品然后在它到达任何其他类别之前停止
答案 0 :(得分:3)
你可以尝试:
SELECT * FROM products WHERE category_id = 1 LIMIT 100
UNION
SELECT * FROM products WHERE category_id = 2 LIMIT 100
UNION
SELECT * FROM products WHERE category_id = 3 LIMIT 100
UNION
SELECT * FROM products WHERE category_id = 4 LIMIT 100
UNION
SELECT * FROM products WHERE category_id = 5 LIMIT 100
这将从1到5返回每个类别的前100条记录。