Datenbanken/Lösung/Aufgabe 4.sql

111 lines
4.8 KiB
MySQL
Raw Normal View History

2022-05-11 14:19:05 +00:00
USE project;
/*
---------------------------------- 1 ----------------------------------
Geben Sie mit SQL alle Menüs mit den darin enthaltenen Produkten menschenlesbar
als eine Art Speisekarte aus.
-----------------------------------------------------------------------
*/
SELECT Menuname, GROUP_CONCAT(Produktname SEPARATOR ', ') AS 'Produkte'
FROM Menu
JOIN Menucontent M on Menu.MenuID = M.MenuID
JOIN Produkt P on M.ProduktID = P.ProduktID
GROUP BY Menuname;
/*
---------------------------------- 2 ----------------------------------
Erweitern Sie die Ausgabe aus der vorherigen Abfrage um hilfreiche Informationen
bezüglich der Gesamtnährwerte dieser Menüs.
-----------------------------------------------------------------------
*/
SELECT Menuname,
GROUP_CONCAT(Produktname SEPARATOR ', ') AS 'Produkte',
2022-05-18 08:51:21 +00:00
SUM(Fett * N.Menge / 100) AS 'Fett',
SUM(Kohlenhydrate * N.Menge / 100) AS 'Kohlenhydrate',
SUM(Eiweiss * N.Menge / 100) AS 'Eiweiss',
SUM(Brennwert * N.Menge / 100) AS 'Brennwert'
2022-05-11 14:19:05 +00:00
FROM Menu
JOIN Menucontent M on Menu.MenuID = M.MenuID
JOIN Produkt P on M.ProduktID = P.ProduktID
JOIN Naehrwerte N on P.ProduktID = N.ProduktID
2022-05-18 08:51:21 +00:00
JOIN Menge M2 on P.ProduktID = M2.ProduktID
2022-05-11 14:19:05 +00:00
GROUP BY Menuname;
/*
---------------------------------- 3 ----------------------------------
Geben sie mit SQL alle Produkte mit für Kundinnen und Kunden relevanten Informationen für
eine bestimmte Person aus (betreffend Allergenen, Unverträglichkeiten, vegetarisch, etc.).
-----------------------------------------------------------------------
2022-05-18 12:25:18 +00:00
TODO Redo
2022-05-11 14:19:05 +00:00
*/
SELECT Personenvorname,
Personenname,
CONCAT_WS(', ', GROUP_CONCAT(DISTINCT Allergenbezeichnung SEPARATOR ', '),
GROUP_CONCAT(DISTINCT Zusatzstoffbezeichnung SEPARATOR ', '))
FROM Person
JOIN Allergie A on Person.PersonID = A.PersonID
JOIN Allergen A2 on A.AllergenID = A2.AllergenID
JOIN Zusatzstoffunvertraeglichkeit Z on Person.PersonID = Z.PersonID
JOIN Zusatzstoff Z2 on Z2.E_Nummer = Z.E_Nummer
GROUP BY Personenvorname, Personenname;
/*
---------------------------------- 4 ----------------------------------
Sortieren Sie alle enthaltenen Produkte absteigend nach Anzahl der enthaltenen Zusatzstoffe.
-----------------------------------------------------------------------
*/
SELECT Produktname,
LENGTH(GROUP_CONCAT(E_Nummer)) - LENGTH(REPLACE(GROUP_CONCAT(E_Nummer), ',', '')) + 1 AS 'Anzahl Zusatzstoffe'
FROM Produkt
JOIN enthaeltZusatzstoff eZ on Produkt.ProduktID = eZ.ProduktID
GROUP BY Produktname
ORDER BY LENGTH(GROUP_CONCAT(E_Nummer)) - LENGTH(REPLACE(GROUP_CONCAT(E_Nummer), ',', '')) + 1 DESC;
/*
---------------------------------- 5 ----------------------------------
2022-05-17 16:22:47 +00:00
Sortieren Sie alle [in der Datenbank (anm. d. St.)] enthaltenen Produkte aufsteigend nach Kalorienanzahl in der Portion, die verkauft wird.
2022-05-17 16:37:20 +00:00
Hinweis: Produkte ohne Brennwert werden nicht angezeigt
2022-05-11 14:19:05 +00:00
-----------------------------------------------------------------------
*/
2022-05-17 16:22:47 +00:00
SELECT Produktname,
ROUND(Brennwert * (M.Menge / 100), 1) AS Brennwert,
M.Menge,
M.Einheit
FROM Produkt
JOIN Naehrwerte N on Produkt.ProduktID = N.ProduktID
JOIN Menge M on Produkt.ProduktID = M.ProduktID
ORDER BY Brennwert;
2022-05-11 14:19:05 +00:00
/*
---------------------------------- 6 ----------------------------------
Geben Sie eine SQL Anfrage an, die alle Produkte ausgibt, die eine bestimmte Anzahl von Emulgatoren enthalten.
a. Keine Emulgatoren
b. 1 Emulgator
c. 2 Emulgatoren
d. Mehr
-----------------------------------------------------------------------
*/
2022-05-18 09:04:26 +00:00
2022-05-18 09:43:08 +00:00
SELECT Produktname,
IFNULL( # wenn feld NULL, Keine Emulgatoren
REPLACE( # wenn feld zero, Keine Emulgatoren
REPLACE( # wenn feld zwei, Zwei Emulgatoren
REPLACE( # wenn feld eins, Ein Emulgator
IF( # wenn feld größer als zwei, Mehr Emulgatoren, ansonsten setze den gleichen wert wieder ins feld
LENGTH(GROUP_CONCAT(eZ.Anwendungsgrund)) - LENGTH(REPLACE(GROUP_CONCAT(eZ.Anwendungsgrund), 'E', ''))
> 2, 'Mehr Emulgatoren',
LENGTH(GROUP_CONCAT(eZ.Anwendungsgrund)) - LENGTH(REPLACE(GROUP_CONCAT(eZ.Anwendungsgrund), 'E', '')))
, 1, 'Ein Emulgator')
, 2, 'Zwei Emulgatoren')
, 0, 'Keine Emulgatoren')
, 'Keine Emulgatoren')
AS Emgulagtoren,
GROUP_CONCAT(eZ.Anwendungsgrund) AS 'Anwendungsgrund'
2022-05-18 09:04:26 +00:00
FROM Produkt
2022-05-18 09:43:08 +00:00
LEFT JOIN enthaeltZusatzstoff eZ on Produkt.ProduktID = eZ.ProduktID
GROUP BY Produktname
ORDER BY LENGTH(GROUP_CONCAT(eZ.Anwendungsgrund)) - LENGTH(REPLACE(GROUP_CONCAT(eZ.Anwendungsgrund), 'E', ''))