Can somebody tell me how i can display all the pizzas and their toppings without displaying the same pizza multiple times with each topping.
name | name |
---|---|
Cajun Combo | bacon crumble |
Cajun Combo | ham |
Cajun Combo | spicy pepperoni |
Cajun Combo | Cajun spice |
Cajun Combo | Fresh garlic |
Cajun Combo | garlic sauce topping |
Bistro | black pepper |
Bistro | cheddar cheese |
Bistro | chili flakes |
Bistro | pepperoni |
Bistro | red onion |
BBQ King | cream cheese |
BBQ King | jalapeno |
BBQ King | pepper cheese |
BBQ King | pepperoni |
BBQ King | pulled pork |
BBQ King | BBQ topping |
Fiesta | cream cheese |
Fiesta | Fresh garlic |
Fiesta | mushrooms |
Fiesta | oregano |
Fiesta | pepperoni |
Fiesta | pineapple |
Italiana | balsamic glaze |
Italiana | cream cheese |
Italiana | dates |
Italiana | pepperoni |
Italiana | semi dried tomatoes |
Italiana | spinach |
Champion | bacon crumble |
Champion | black pepper |
Champion | Fresh garlic |
Champion | ham |
Champion | jalapeno |
Champion | mushrooms |
Champion | pepperoni |
Champion | pineapple |
Champion | red onion |
CREATE TABLE toppings (
idTopping INTEGER PRIMARY KEY,
name text
);
CREATE TABLE pizzas (
idPizza INTEGER PRIMARY KEY,
name text
);
CREATE TABLE pizzasAndToppings (
idTopping INTEGER,
idPizza INTEGER,
FOREIGN KEY (idTopping)
REFERENCES toppings (idTopping),
FOREIGN KEY (idPizza)
REFERENCES pizzas (idPizza)
);
INSERT INTO pizzasAndToppings (idTopping, idPizza) VALUES (5, 1), (7, 1), (12, 1), (27, 1), (29, 1), (30, 1);
INSERT INTO pizzasAndToppings (idTopping, idPizza) VALUES (26, 2), (1, 2), (28, 2), (9, 2), (21, 2);
INSERT INTO pizzasAndToppings (idTopping, idPizza) VALUES (2, 3), (18, 3), (4, 3), (9, 3), (11, 3), (25, 3);
INSERT INTO pizzasAndToppings (idTopping, idPizza) VALUES (2, 4), (29, 4), (19, 4), (31, 4), (9, 4), (20, 4);
INSERT INTO pizzasAndToppings (idTopping, idPizza) VALUES (24, 5), (2, 5), (15, 5), (9, 5), (22, 5), (23, 5);
INSERT INTO pizzasAndToppings (idTopping, idPizza) VALUES (5, 6), (26, 6), (29, 6), (7, 6), (18, 6), (19, 6), (9, 6), (20 ,6), (21, 6);
INSERT INTO toppings (name) VALUES ('cheddar cheese'), ('cream cheese'), ('Havarti'), ('pepper cheese'),
('bacon crumble'), ('chicken fajitas'), ('ham'), ('minced beef'), ('pepperoni'), ('prime bacon slices'), ('pulled pork'),
('spicy pepperoni'), ('Vegan chicken'), ('black olives'), ('dates'), ('fresh chili'), ('green pepper'), ('jalapeno'), ('mushrooms'),
('pineapple'), ('red onion'), ('semi dried tomatoes'), ('spinach'), ('balsamic glaze'), ('BBQ topping'), ('black pepper'),
('Cajun spice'), ('chili flakes'), ('Fresh garlic'), ('garlic sauce topping'), ('oregano');
INSERT INTO pizzas (name) VALUES ('Cajun Combo'), ('Bistro'), ('BBQ King'), ('Fiesta'), ('Italiana'), ('Champion');
/* SELECT */
SELECT pizzas.name, toppings.name FROM pizzas INNER JOIN pizzasAndToppings ON pizzasAndToppings.idPizza = pizzas.idPizza INNER JOIN toppings ON toppings.idTopping = pizzasAndToppings.idTopping;
CodePudding user response:
You can use the group_concat
aggregate function:
SELECT pizzas.name AS pizza_name,
GROUP_CONCAT(toppings.name) AS toppings_names
FROM pizzas
INNER JOIN pizzasAndToppings ON pizzasAndToppings.idPizza = pizzas.idPizza
INNER JOIN toppings ON toppings.idTopping = pizzasAndToppings.idTopping
GROUP BY pizzas.name