我为以前的问题/问题获得了帮助,并且刚刚意识到给我的脚本返回了“错误”的数字/值(但不会触发 php/mysql 错误)。这是值:
成分#1:20.216卡路里
成分#2:134.4564卡路里
脚本应返回:154.67,但返回:91.35
编码:
<?php
//The Recipe selected
$recipe_id = $_POST['recipe_id'];
/*****************************************
 * Total CALORIES in this recipe
 *****************************************/
echo '<h4>Total calories in this recipe:</h4>';
$result = mysql_query(
  "SELECT SUM(ingredient_calories), ingredient_amount
  FROM recipe_ingredients
  INNER JOIN ingredients USING (ingredient_id)
  WHERE recipe_id = $recipe_id");
while ($row = mysql_fetch_array($result)) {
  $recipe_calories = (
    $row['SUM(ingredient_calories)']
    * $row['ingredient_amount']);
}
echo number_format($recipe_calories, 2);
mysql_free_result($result);
?>
成分表存储每种成分的默认值和份量。
recipes表,存储/设置 recipe_id 和其他与查询无关的数据)
recipe_ingredients表映射配方中的成分并存储配方中使用的量。
我确定这是查询的问题,但我耳朵后面太湿了,无法确定问题。任何帮助是极大的赞赏 :)
更新:这是表数据,根据要求
ingredients
-----------
ingredient_id
ingredient_name
ingredient_calories
recipes
-------
recipe_id
recipe_name
recipe_ingredients
------------------
recipe_id (fk)
ingredient_id (fk)
ingredient_amount