进行一个查询以连接多个表

时间:2016-01-04 15:23:26

标签: mysql join

我有3张桌子:

  1. 营养表,它是具有所有营养nutrition_idnutrition_name
  2. 的静态表
  3. 包含产品信息且列为product_idproduct_namebrand
  4. 的产品表
  5. product_nutritions这是一个中间人表,它将产品与营养成分及其价值联系起来。结构为product_idnutrition_idnutrition_value。每种产品可以有1行或更多,具体取决于它具有的营养成分的数量。
  6. 这是一个真实的测试示例

    营养表

    nutrition_id |nutrition_name
               1 | caloreis    
               2 | fat    
               3 | sugar    
               4 | salt
    

    产品表

    product_id| product_name           | brand   
            1 | Nutella Hazelnut Cocoa | Nutella    
            2 | Nutella Jar            | Nutella
    

    product_nutrition table

    product_id | nutrition_id | nutrition_value
             1 |            1 |             200
             1 |            2 |              15
             1 |            3 |               2
             1 |            4 |              11
             2 |            1 |             200
             2 |            2 |              15
             2 |            3 |              12
             2 |            4 |              11
    

    但我需要创建一个查询,返回产品名称以及属于特定品牌的营养成分,其中糖的价值小于5.

    以下是我尝试的查询

     SELECT * 
     FROM products pi
     INNER JOIN product_nutrition_facts pn ON pi.product_id = pn.product_id
     WHERE pn.nutrition_id
     IN (
     '2,4,1'
     )
     OR (
      pn.nutrition_value <=5
      AND pn.nutrition_id =3
     )
     HAVING p.brand ='Nutella'
     ORDER BY `pn`.`nutrition_id` DESC 
     LIMIT 0 , 10
    

    该代码返回营养成分标签中甚至没有糖的产品!

    有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

试试这个:

SELECT 
    p.brand,
    p.product_name,
    n.nutrition_name,
    pn.nutrition_value
FROM
    product_nutritions pn,
    Products p,
    Nutritions n
WHERE
    pn.nutrition_id = n.nutrition_id
        AND pn.product_id = p.product_id
        AND p.brand = 'Nutella'
        AND n.nutrition_name = 'sugar'
        AND pn.nutrition_value <= 5;

编辑:试试这个: SELECT p.brand, p.product_name, n.nutrition_name, pn.nutrition_value FROM product_nutritions pn, Products p, Nutritions n WHERE pn.nutrition_id = n.nutrition_id AND pn.product_id = p.product_id AND p.id IN ( SELECT p.id FROM product_nutritions pn, Products p, Nutritions n WHERE pn.nutrition_id = n.nutrition_id AND pn.product_id = p.product_id AND p.brand = 'Nutella' AND n.nutrition_name = 'sugar' AND pn.nutrition_value <= 5 );

对于多种营养素,您可以这样做: SELECT p.brand, p.product_name, n.nutrition_name, pn.nutrition_value FROM product_nutritions pn, Products p, Nutritions n WHERE pn.nutrition_id = n.nutrition_id AND pn.product_id = p.product_id AND p.id IN ( SELECT p.id FROM product_nutritions pn, Products p, Nutritions n WHERE pn.nutrition_id = n.nutrition_id AND pn.product_id = p.product_id AND ( ( p.brand = 'Nutella' AND n.nutrition_name = 'sugar' AND pn.nutrition_value <= 5 ) OR ( p.brand = 'Nutella' AND n.nutrition_name = 'whatever' AND pn.nutrition_value <= whatevernumber ) );

`

答案 1 :(得分:0)

答案 2 :(得分:0)

使用JOIN声明

SELECT products.name as productName, nutrition.nutrition_name as NutritionName, product_nutrition.nutrition_value 
FROM product_nutrition 
INNER JOIN products on products.id = product_nutrition.product_id 
INNER JOIN Nutritions on Nutritions.id=product_nutrition.nutrition_id 
WHERE product_nutrition.nutrition_value = 'any value you want'