gpt4 book ai didi

mysql - 连接2个表进行查看

转载 作者:行者123 更新时间:2023-11-29 21:41:17 28 4
gpt4 key购买 nike

我有 2 个食谱表,以及recipe_ ratings
我不知道如何从 Recipe_Rating 获取 AVG(Rating_Value)
并加入他们

食谱表(获取'*')并用AVG(recipe_value)连接
按发布日期订购

最佳答案

这是我的方法:

表格和数据:

CREATE TABLE recipes 
(`id` int, `recipe_name` varchar(7))
;
CREATE TABLE recipe_ratings
(`id` int, `recipeId` int,`rating_value` int,`date_posted` datetime)
;

INSERT INTO recipes
(`id`, `recipe_name`)
VALUES
(1, 'Cake'),
(2, 'Bun'),
(3, 'Hot Dog'),
(4, 'Tea')
;

INSERT INTO recipe_ratings
(`id`, `recipeId`,`rating_value`,`date_posted`)
VALUES
(1,1,3,'2015-03-20 10:30:00.000'),
(2,2,4,'2015-06-20 10:30:00.000'),
(3,3,5,'2014-03-20 10:30:00.000')
;

查询:

SELECT R.*, AVG(RR.rating_value) rating_value
FROM recipes R
LEFT JOIN recipe_ratings RR ON R.id = RR.recipeId
GROUP BY RR.date_posted,R.id
ORDER BY RR.date_posted DESC

SqlFiddle Demo

关于mysql - 连接2个表进行查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34469769/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com