sql group by-诅咒和祝福

时间:2018-11-22 11:35:12

标签: mysql sql mysqli

嘿,我有3张桌子用于计算膳食。

CREATE TABLE Meals (
    meal_id int,
    name text
);

CREATE TABLE Ingredients (
    ingredient_id int,
    name text,
    minamount float,
    price float
);

CREATE TABLE Recipe (
    meal_id int,
    ingredient_id int,
    quantitiy float,
    ingredientName text
);

现在我想按食材找一顿饭

SELECT m.meal_id, 
                    m.name,
                    r.ingredientName
                    from meals m
                    join recipe r on m.meal_id = r.meal_id
                    join ingredients i on i.ingredient_id = r.ingredient_id
                    where r.ingredientName = 'meat'
                    group by m.meal_id

给我一​​个像这样的列表

name, ingredientName
Lasagne, meat
Bolognese, meat

我也喜欢按以下价格找到餐点:

SELECT m.meal_id, 
                    m.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
                    GROUP BY m.meal_id, 
                             m.name
                    HAVING mealPrice > 5.0 AND mealPrice < 8.0 

给我一​​个像这样的列表

name, mealPrice
Lasagne, 6.5
Bolognese, 7.8

现在让我建立一个类似的查询

SELECT m.meal_id, 
                    m.name,
                    r.ingredientName,
                    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
                    where r.ingredientName = 'meat'
                    GROUP BY m.meal_id
                             r.ingredientName  
                    HAVING mealPrice > 5.0 AND mealPrice < 8.0

请给我一个类似的列表:

name, ingredientName, mealPrice
Lasagne, meat, 1.99
Bolognese, meat, 1.99

因为这顿肉餐的价格(i.price / i.minamount * r.quanitity)为1.99

没有“ GROUP BY r.ingredientName”,它仅返回一个值

name, ingredientName, mealPrice
Lasagne, meat, 6.5

但是我需要像这样的列表:

name, ingredientName, mealPrice
Lasagne, meat, 6.5
Bolognese, meat, 7.8

我真的是SQL的新手(需要在我的Android项目的API中使用此查询。),我不知道如何为此建立查询,我是否需要变量或子查询之类的东西?不知道......

2 个答案:

答案 0 :(得分:0)

尝试

                SELECT m.meal_id, 
                m.name,
                r.ingredientName,
                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
                where r.ingredientName = 'meat'
                GROUP BY m.meal_id, r.ingredientName  
                HAVING  mealPrice > 5.0 AND mealPrice < 8.0

答案 1 :(得分:0)

也许添加一个EXISTS只能在食谱中添加带有“肉”的食物。

SELECT 
 m.meal_id, m.name,
 SUM((i.price / i.minamount) * r.quantity) as mealPrice
FROM meals m
JOIN recipe r ON r.meal_id = m.meal_id
JOIN ingredients i ON i.ingredient_id = r.ingredient_id
WHERE EXISTS
(
  SELECT 1
  FROM recipe r2
  WHERE r2.ingredientName = 'meat'
    AND r2.meal_id = m.meal_id
)
GROUP BY m.meal_id, m.name
HAVING mealPrice BETWEEN 5.01 AND 7.99