logical-grouping

PHP/MySQL group results by column

故事扮演 提交于 2019-11-30 16:25:40
in order to keep as few SQL statements as possible, I want to do select set from MySQL: SELECT * FROM products WHERE category IN (10,120,150,500) ORDER BY category,id; Now, I have list of products in following manner: CATEGORY - product 1 - product 2 CATEGORY 2 - product 37 ... What's the best and most efficent way to process MySQL result? I thought something like (pseudo PHP) foreach ($product = fetch__assoc($result)){ $products[$category][] = $product; } and then when outputting it, do foreach loop: foreach($categories as $category){ foreach($products[$category] as $product){ $output; } } Is

PHP/MySQL group results by column

柔情痞子 提交于 2019-11-29 22:59:30
问题 in order to keep as few SQL statements as possible, I want to do select set from MySQL: SELECT * FROM products WHERE category IN (10,120,150,500) ORDER BY category,id; Now, I have list of products in following manner: CATEGORY - product 1 - product 2 CATEGORY 2 - product 37 ... What's the best and most efficent way to process MySQL result? I thought something like (pseudo PHP) foreach ($product = fetch__assoc($result)){ $products[$category][] = $product; } and then when outputting it, do