[英]How to return multiple rows in a LEFT JOIN
我遇到一种情况,可以说我正在尝试获取有关某些食物的信息。 然后,我需要显示所有信息以及该食物中的所有成分。
通过我的查询,我以数组的形式获取了所有信息,但只有第一个要素...
myFoodsArr =
[0]
foodDescription = "the description text will be here"
ratingAverage = 0
foodId = 4
ingredient = 1
ingAmount = 2
foodName = "Awesome Food name"
typeOfFood = 6
votes = 0
我想像这样回来
myFoodsArr =
[0]
foodDescription = "the description text will be here"
ratingAverage = 0
foodId = 4
ingArr = {ingredient: 1, ingAmount: 4}, {ingredient: 3, ingAmount: 2}, {ingredient: 5, ingAmount: 1}
foodName = "Awesome Food name"
typeOfFood = 6
votes = 0
这是即时消息正在使用的查询。 如何调整此值以返回食物ID 4,然后还获得该食物的所有成分? 在同一时间做其他事情,例如获得该食物的平均评分吗?
谢谢!
SELECT a.foodId, a.foodName, a.foodDescription, a.typeOfFood, c.ingredient, c.ingAmount, AVG(b.foodRating) AS ratingAverage, COUNT(b.foodId) as tvotes
FROM `foods` a
LEFT JOIN `foods_ratings` b
ON a.foodId = b.foodId
LEFT JOIN `foods_ing` c
ON a.foodId=c.foodId
WHERE a.foodId=4
编辑:
Catcall引入了我从未听说过的“子查询”这一概念,因此我正在尝试进行这项工作,以查看是否可以轻松地在1个查询中完成此操作。 但我只是不断得到错误的回报。 这就是我没有运气的尝试。
//I changed some of the column names to help them be more distinct in this example
SELECT a.foodId, a.foodName, a.foodDescription, a.typeOfFood, AVG(b.foodRating) AS ratingAverage, COUNT(b.foodId) as tvotes
FROM foods a
LEFT JOIN foods_ratings b ON a.foodId = b.foodId
LEFT JOIN (SELECT fId, ingredientId, ingAmount
FROM foods_ing
WHERE fId = 4
GROUP BY fId) c ON a.foodId = c.fId
WHERE a.foodId = 4";
编辑另外1条与ROLANDS GROUP_CONCAT / JSON Idea相关的内容作为解决方案4
我试图确保发送回我的Flash项目的JSON字符串已准备好正确解析Invalid JSON parse input.
不断弹出..
所以我想我需要在正确的位置适当地包含所有双引号。
但是在我的MySQL查询字符串中,我试图转义双引号,但是这样会使我的mySQL var无法工作,例如...
如果我这样做..
GROUP_CONCAT('{\"ingredient\":', \"c.ingredient\", ',\"ingAmount\":', \"c.ingAmount\", '}')`
我明白了...
{"ingredient":c.ingredient,"ingAmount":c.ingAmount},{"ingredient":c.ingredient,"ingAmount":c.ingAmount},{"ingredient":c.ingredient,"ingAmount":c.ingAmount}
如何在不破坏mysql的情况下使用所有双引号使JSON正确形成?
这应该可以解决问题:
SELECT food_ingredients.foodId
, food_ingredients.foodName
, food_ingredients.foodDescription
, food_ingredients.typeOfFood
, food_ingredients.ingredients
, AVG(food_ratings.food_rating) food_rating
, COUNT(food_ratings.foodId) number_of_votes
FROM (
SELECT a.foodId
, a.foodName
, a.foodDescription
, a.typeOfFood
, GROUP_CONCAT(
'{ingredient:', c.ingredient,
, ',ingAmount:', c.ingAmount, '}'
) ingredients
FROM foods a
LEFT JOIN foods_ing c
ON a.foodsId = c.foodsId
WHERE a.foodsId=4
GROUP BY a.foodId
) food_ingredients
LEFT JOIN food_ratings
ON food_ingredients.foodId = food_ratings.foodId
GROUP BY food_ingredients.foodId
请注意,您要执行的查询类型在任何基于SQL的数据库中都不是简单的。
主要问题是您拥有一个具有两种详细信息(成分和等级)的主食(食物)。 因为这些细节彼此无关(除了与母版无关),所以它们彼此形成笛卡尔积(仅受它们与母版的关系约束)。
上面的查询通过以下两个步骤解决了这一问题:首先,连接到第一个详细信息(成分)并聚合详细信息(使用group_concat制作所有相关成分行的一行),然后将结果连接到第二个详细信息(评分)并再次汇总。
在上面的示例中,成分以结构化字符串的形式返回,就像在您的示例中出现的一样。 如果要访问PHP内的数据,则可以考虑添加更多语法以使其成为有效的JSON字符串,以便可以使用php函数json_decode()
将其解码为数组: http : json_decode()
/manual/zh-CN/function.json-decode.php
为此,只需将行更改为:
CONCAT(
'['
, GROUP_CONCAT(
'{"ingredient":', c.ingredient
, ',"ingAmount":', c.ingAmount, '}'
)
, ']'
)
(这假定ingredient
和ingAmount
为数字;如果它们是字符串,则应将其双引号,并转义出现在字符串值内的所有双引号)
如果您为group_concat_max_len
服务器变量保留默认设置, group_concat_max_len
成分与GROUP_CONCAT
串联可能会导致问题。 减轻该问题的一种简单方法是将其设置为任何结果的最大理论大小:
SET group_concat_max_len = @@max_allowed_packet;
打开与mysql的连接后,您可以执行一次此操作,然后在该会话期间有效。 另外,如果您具有超级特权,则可以为整个MySQL实例全面更改值:
SET GLOBAL group_concat_max_len = @@max_allowed_packet;
您还可以在my.cnf或my.ini中添加一行以将group_concat_max_lenght设置为任意任意的足够大的静态值。 参见http://dev.mysql.com/doc/refman/5.5/zh-CN/server-system-variables.html#sysvar_group_concat_max_len
一种显而易见的解决方案是实际执行两个查询:
1)拿到食物
SELECT a.foodId, a.foodName, a.foodDescription, a.typeOfFood
FROM `foods` a
WHERE a.foodsId=4
2)获取所有成分
SELECT c.ingredient, c.ingAmount
FROM `foods_ing` c
WHERE c.foodsId=4
这种方法的优点是您不会将“食物”表中的数据复制到结果中。 缺点是您必须执行两个查询。 实际上,您必须对每种“食物”执行一个额外的查询,因此,如果要列出所有食物及其成分的清单,则必须对每个食物记录进行查询。
其他解决方案通常具有许多缺点,其中之一是使用GROUP_CONCAT函数,但是它对返回的字符串的长度有严格的限制。
当您将MySQL的聚合函数和GROUP BY行为与SQL标准进行比较时,您必须得出结论,它们只是被破坏了。 您可以在单个查询中执行所需的操作,但是您需要加入返回返回聚合函数结果的查询,而不是直接加入到评分表中。 遵循这些原则的东西应该起作用。
select a.foodId, a.foodName, a.foodDescription, a.typeOfFood,
c.ingredient, c.ingAmount,
b.numRatings, b.avgRating
from foods a
left join (select foodId, count(foodId) numRatings, avg(foodRating) avgRating
from foods_ratings
group by foodId) b on a.foodId = b.foodId
left join foods_ing c on a.foodId = c.foodId
order by a.foodId
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.