我如何在PHP而不是MySQL中分组和group_concat?

时间:2013-02-04 21:13:45

标签: php mysql

我有一个SQL查询:

SELECT mealdesc, group_concat(fooddesc) as fooddesc 
FROM plan p
INNER JOIN mealplan mp
ON mp.planid = p.id
INNER JOIN meal m
ON m.id = mp.mealid
INNER JOIN foodmeal fm
ON fm.mealid = m.id
INNER JOIN food f
ON f.id = fm.foodid
where userid = 2
GROUP by mealdesc
order by mealdesc

结果如下:

mealdesc,fooddesc
Meal 1,"egg,egg whites,oatmeal,wheat toast,fruit,berries"
Meal 2,"Isolyze,raw nuts"
Meal 3,"chicken,turkey,yam,baked potato,veggies,medium salad"
Meal 4,"Isolyze,raw nuts"
Meal 5,"veggies,lean red meat,salmon,Chilean Sea Bass,large salad"
Meal 6,"veggies,large salad,Casein Protein,white fish"

当我删除group by和group_concat时,结果如下所示:

mealdesc,fooddesc
Meal 1,egg
Meal 1,egg whites
Meal 1,oatmeal
Meal 1,wheat toast
Meal 1,fruit
Meal 1,berries
Meal 2,Isolyze
Meal 2,raw nuts
Meal 3,veggies
Meal 3,medium salad
Meal 3,chicken
Meal 3,turkey
...and so on.

而不是在sql语句中执行Group by和Group_Concat并且必须处理explode等来处理分隔符并将其转换为php数组,是否有办法执行相同的group by和group_concact逻辑和用PHP组织所有这些并将其推入数组?

更新: 感谢您到目前为止的回复。将PHP和HTML部分添加到此。

我基本上希望将每个膳食作为标题和每个食物列在列表中。我过去曾经使用过爆炸,但我不能正确地形成阵列,也许这只是我的问题。

PHP代码:

$sql = "SELECT mealdesc, group_concat(fooddesc) as fooddesc 
    FROM plan p
    INNER JOIN mealplan mp
    ON mp.planid = p.id
    INNER JOIN meal m
    ON m.id = mp.mealid
    INNER JOIN foodmeal fm
    ON fm.mealid = m.id
    INNER JOIN food f
    ON f.id = fm.foodid
    where userid = 2
    GROUP by mealdesc
    order by mealdesc";

$result = mysqli_query($link, $sql);
if (!$result)
{
echo 'Error';
exit();
}


// Fetch food data
while($row = mysqli_fetch_assoc($result)){
$meals[] = array('mealdesc' => $row['mealdesc'], 'fooddesc' => explode(",", $row['fooddesc']));
}


echo '<pre>';
echo print_r($meals, true);
echo '</pre>';

然后在我的HTML中循环通过这样输出膳食和食物描述

<?php foreach ($meals as $meal): ?>
<h4><?php htmlout($meal['mealdesc']); ?></h4>
<ul>
  <li><?php htmlout($meal['fooddesc']); ?></li>
</ul>
<?php endforeach; ?>

2 个答案:

答案 0 :(得分:1)

如果你想以这种方式保留SQL,为什么不在PHP中做这样的事情:

  while($row = dbOBj->Fetch($result))
    {
    $meal[$row['meal desc'][] = $row['food desc'];
    }

你最终会得到$meal['Meal 1'] = array('egg', 'egg whites', ...)

答案 1 :(得分:0)

我假设您使用PDO通过准备好的声明获取上面的记录集:

$meals = array();
while ($row = $stmt->fetch()) {
    $meals[$row[0]][] = $row[1];
}

这为您提供以下格式:

array(
    "Meal 1" => array("egg","egg whites","oatmeal","wheat toast","fruit","berries"),
    "Meal 2" => array("Isolyze","raw nuts"),
    "Meal 3" => array("chicken","turkey","yam","baked potato","veggies","medium salad"),
    "Meal 4" => array("Isolyze","raw nuts"),
    "Meal 5" => array("veggies","lean red meat","salmon","Chilean Sea Bass","large salad"),
    "Meal 6" => array("veggies","large salad","Casein Protein","white fish"),
);

然后,您可以按照您希望的格式选择一个:

implode(",", $meals["Meal 1"]);

输出

egg,egg whites,oatmeal,wheat toast,fruit,berries

修改

查看上面的代码,您可能希望在HTML中执行以下操作:

<?php foreach ($meals as $meal): ?>
<h4><?php htmlout($meal['mealdesc']); ?></h4>
<ul>
  <?php foreach ($meal['fooddesc'] as $d): ?>
  <li><?php htmlout($d); ?></li>
  <?php endforeach; ?>
</ul>
<?php endforeach; ?>