[英]MySQL Selecting records by multiple IDs with LIMIT per each ID 4
我被困在查詢中。 場景是這樣的: - 我需要為每個類別選擇最多 4 個 product_name。 - 我正在使用這個查詢,但它沒有按照我需要的方式工作 -each cat_id with product_name limit 4
結果將包含來自整個查詢輸出的 4 行。
$sql_course = "SELECT * FROM product WHERE cat_id IN('".$brand_filter."') ORDER BY cat_id LIMIT 4";
我要這個
cat_id1- product1
cat_id1- product2
cat_id1- product3
cat_id1- product4
cat_id2- product5
cat_id2- product6
cat_id2- product7
cat_id2- product8
限制正在影響您的輸出,因為它只帶來四個記錄。
$sql_course = "SELECT * FROM product WHERE cat_id IN('".$brand_filter."') GROUP BY cat_id ORDER BY cat_id LIMIT 4";
我想這會奏效,請嘗試檢查結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.