I have these two tables:
meal:
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
ingredient:
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
The meal_id field comes from the meal table.
I have some data filled already:
+----+------------+-----------+
| 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 |
+----+--------------+----------+--------+-----+----------------+---------+
My current query is:
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;
which gives:
+------------+-----------+--------------+----------+--------+-----+----------------+
| 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 |
+------------+-----------+--------------+----------+--------+-----+----------------+
The problem is that in this case there are 2 ingredients for a meal so the meal name is printed twice. Is there any way to display meal_name and category once and related ingredients in a same row?
Thanks! Cheers!