我真的是SQL新手-从1周开始,我的查询仅适用于1条记录
ast = pd.read_csv(directory,encoding = 'UTF-8')
inte = pd.read_csv(directory,encoding = 'UTF-8')
ast.reset_index(inplace= True)
ast.rename(columns = {'index': 'FID'},inplace = True)
final = pd.merge(ast, inte, on = ['FID'], how = 'inner')
final
此查询仅适用于一条记录,但是如果我想获取所有餐点(不带“ and”功能),我仍然只获得一条记录,但是sumpricePrice是添加的所有记录的总和,如何获取此查询一顿饭吗?
我喜欢每顿饭一个总价的记录
我的桌子:
Select m.meal_id, m.name, m.usp, m.description, m.worktime, m.proprietor, m.img, m.url, m.servings, d.name, c.name, s.name, sum(i.price / i.minamount * r.quantity) as mealPrice
from meals m
join recipe r on m.meal_id = r.meal_id
join ingredients i on i.ingredient_id = r.ingredient_id
join difficulty d on d.difficulty_id = m.difficulty_id
join cat c on c.cat_id = m.cat_id
join social s on s.social_id = m.social_id
and m.meal_id = 1
答案 0 :(得分:0)
编辑:基于OP的recent edit;您只需做一个GROUP BY
。
尝试:
Select m.meal_id,
m.name,
m.usp,
m.description,
m.worktime,
m.proprietor,
m.img,
m.url,
m.servings,
d.name,
c.name,
s.name,
SUM(i.price / i.minamount * r.quantity) as mealPrice
from meals m
join recipe r on m.meal_id = r.meal_id
join ingredients i on i.ingredient_id = r.ingredient_id
join difficulty d on d.difficulty_id = m.difficulty_id
join cat c on c.cat_id = m.cat_id
join social s on s.social_id = m.social_id
GROUP BY m.meal_id,
m.name,
m.usp,
m.description,
m.worktime,
m.proprietor,
m.img,
m.url,
m.servings,
d.name,
c.name,
s.name
基于OP问题的Previous Version:
这是一个Window function问题。在较新版本的MySQL(> = 8.0 )中,您可以轻松地做到这一点。在较旧的版本(您的版本为5.5)中,我们可以使用Session Variables来解决它。
尝试:
Select m.meal_id,
m.name,
m.usp,
m.description,
m.worktime,
m.proprietor,
m.img,
m.url,
m.servings,
d.name,
c.name,
s.name,
(@sum := @sum + (i.price / i.minamount * r.quantity)) as mealPrice
from meals m
join recipe r on m.meal_id = r.meal_id
join ingredients i on i.ingredient_id = r.ingredient_id
join difficulty d on d.difficulty_id = m.difficulty_id
join cat c on c.cat_id = m.cat_id
join social s on s.social_id = m.social_id
CROSS JOIN (select @sum := 0) AS init
在MySQL版本> = 8.0中,我们可以在完整结果集的分区上将SUM()
用作Window函数。在这种情况下,查询将是:
Select m.meal_id,
m.name,
m.usp,
m.description,
m.worktime,
m.proprietor,
m.img,
m.url,
m.servings,
d.name,
c.name,
s.name,
SUM(i.price / i.minamount * r.quantity) OVER() as mealPrice
from meals m
join recipe r on m.meal_id = r.meal_id
join ingredients i on i.ingredient_id = r.ingredient_id
join difficulty d on d.difficulty_id = m.difficulty_id
join cat c on c.cat_id = m.cat_id
join social s on s.social_id = m.social_id
答案 1 :(得分:0)
您需要在下面的查询中,交叉联接有助于您获得所需的结果
Select m.meal_id,
m.name,
m.usp,
m.description,
m.worktime,
m.proprietor,
m.img,
m.url,
m.servings,
d.name,
c.name,
s.name,
(@s := @s + (i.price / i.minamount * r.quantity)) as mealPrice
from meals m
join recipe r on m.meal_id = r.meal_id
join ingredients i on i.ingredient_id = r.ingredient_id
join difficulty d on d.difficulty_id = m.difficulty_id
join cat c on c.cat_id = m.cat_id
join social s on s.social_id = m.social_id
CROSS JOIN ( select @s:= 0) AS t