SQL doubled导致查询2个连接相同id的表

时间:2018-03-24 11:19:54

标签: mysql sql

我有这两个表:

的膳食:

CREATE TABLE `meal` (
  `id` int(6) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `category` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB

成分:

 CREATE TABLE `ingredient` (
  `id` int(6) unsigned NOT NULL AUTO_INCREMENT,
  `product_name` varchar(30) NOT NULL,
  `proteins` varchar(30) NOT NULL,
  `carbos` varchar(30) NOT NULL,
  `fat` varchar(30) NOT NULL,
  `total_calories` varchar(30) NOT NULL,
  `meal_id` int(6) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB

meal_id 字段来自餐桌。

我已经填写了一些数据:

+----+------------+-----------+
| id | name       | category  |
+----+------------+-----------+
| 46 | ser        | sniadanie |
| 47 | pizza      | kolacja   |
| 48 | tagiatelle | obiad     |
| 49 | tagiatelle | obiad     |
| 50 | tagiatelle | obiad     |
+----+------------+-----------+


+----+--------------+----------+--------+-----+----------------+---------+
| id | product_name | proteins | carbos | fat | total_calories | meal_id |
+----+--------------+----------+--------+-----+----------------+---------+
| 61 | indyk        | 2        | 2      | 2   | 2              |      46 |
| 62 | makaron      | 2        | 2      | 2   | 2              |      46 |
| 63 | indyk        | 2        | 2      | 2   | 2              |      47 |
| 64 | makaron      | 2        | 2      | 2   | 2              |      47 |
| 65 | indyk        | 2        | 2      | 2   | 2              |      48 |
| 66 | makaron      | 2        | 2      | 2   | 2              |      48 |
| 67 | indyk        | 2        | 2      | 2   | 2              |      49 |
| 68 | makaron      | 2        | 2      | 2   | 2              |      49 |
| 69 | indyk        | 2        | 2      | 2   | 2              |      50 |
| 70 | makaron      | 2        | 2      | 2   | 2              |      50 |
+----+--------------+----------+--------+-----+----------------+---------+

我目前的查询是:

SELECT meal.name AS 'meal_name', meal.category, ingredient.product_name,
    ingredient.proteins, ingredient.carbos, ingredient.fat, 
    ingredient.total_calories 
FROM meal 
JOIN ingredient WHERE meal.id = ingredient.meal_id;

给出:

    +------------+-----------+--------------+----------+--------+-----+----------------+
| meal_name  | category  | product_name | proteins | carbos | fat | total_calories |
+------------+-----------+--------------+----------+--------+-----+----------------+
| ser        | sniadanie | indyk        | 2        | 2      | 2   | 2              |
| ser        | sniadanie | makaron      | 2        | 2      | 2   | 2              |
| pizza      | kolacja   | indyk        | 2        | 2      | 2   | 2              |
| pizza      | kolacja   | makaron      | 2        | 2      | 2   | 2              |
| tagiatelle | obiad     | indyk        | 2        | 2      | 2   | 2              |
| tagiatelle | obiad     | makaron      | 2        | 2      | 2   | 2              |
| tagiatelle | obiad     | indyk        | 2        | 2      | 2   | 2              |
| tagiatelle | obiad     | makaron      | 2        | 2      | 2   | 2              |
| tagiatelle | obiad     | indyk        | 2        | 2      | 2   | 2              |
| tagiatelle | obiad     | makaron      | 2        | 2      | 2   | 2              |
+------------+-----------+--------------+----------+--------+-----+----------------+

问题在于,在这种情况下,有两种食物成分,因此膳食名称被打印两次。有没有办法在同一行显示meal_name和类别及相关成分?

谢谢!干杯!

2 个答案:

答案 0 :(得分:-1)

SELECT meal.name AS 'meal_name', meal.category, 
GROUP_CONCAT(ingredient.product_name) AS product, 
GROUP_CONCAT(ingredient.proteins) AS proteins, 
GROUP_CONCAT(ingredient.carbos) AS carbos, GROUP_CONCAT(ingredient.fat) 
AS fat, GROUP_CONCAT(ingredient.total_calories) AS calories FROM meal 
JOIN ingredient WHERE meal.id = ingredient.meal_id GROUP BY 
ingredient.meal_id;

答案 1 :(得分:-1)

以下是您解决问题的方法:

SELECT Meal_Name,Category,Product_Name,Carbos,Fat,Total_Calories
FROM (
  SELECT meal_id,meal.name AS 'meal_name', meal.category, 
  ingredient.proteins, ingredient.carbos, ingredient.fat,
  ingredient.total_calories,
  GROUP_CONCAT(ingredient.product_name SEPARATOR ',') AS Product_name
  FROM meal 
  JOIN ingredient 
  WHERE meal.id = ingredient.meal_id 
  GROUP BY meal_id,meal_name,category,proteins,carbos,fat,total_calories
 ) AS Tbl
;     

点击演示链接:

  

http://www.sqlfiddle.com/#!9/36f55/11