Hallo Ich habe Datenbank mit Tabellen: Produkte, Produktfarben, Farben.SQL Wie wählen Eltern ID oder ID (wenn nicht Eltern) Argumente
Produkte haben Attribut: id, name, ...
Product_colors haben Attribut: id, products_id, colors_id
Farben Attribut: id, colors_id , Name
so dass Produkt Reihe in Produktfarben haben, indem du products.id = product_colors.products_id und nächste Verbindung colors_id über product_colors.colors_id = colors.id
Produkte:
id name
-------------------
1 produkt1
2 produkt2
3 produkt3
Product_colors:
id products_id colors_id
1 1 1
2 1 5
3 2 1
4 2 3
5 3 6
Farben können Eltern über colors_id haben:
id colors_id name
__________________________________
1 null red
2 null green
3 1 flashred
4 2 kiwi
5 2 lightgreen
6 null black
Red -> flashred, orange
Grün -> Kiwi, hellgrün, dunkelgrün
Wie kann ich alle Produkte erhalten, die alle Eltern Farben enthält (wie parametr), wenn Argumente Farben rot und hellgrün -> Als ausgewähltes Produkt sollte Farben rot und grün haben genau
So brauche ich Eltern der Argumente Farbe (wenn Argument nicht Elternteil Ich nehme Argument Farbe), geben diese Eltern Farben Array von diesem Array usin IN ausgewählte Produkte. ich dies möchte in MYSQL
SELECT products.id AS id
FROM products
LEFT JOIN product_colors
ON products.id = product_colors.products_id
LEFT JOIN colors
ON product_colors.colors_id = colors.id
WHERE colors.id IN (1,5)
1,5 Argumente sind
Für Argumente [1,5] Ich möchte Ergebnis Produkt mit ID 1
Siehe COALESCE(). – Strawberry
Bitte teilen Sie das erwartete Ergebnis. –
Kumar_Vikas ja Ich habe Beispieltabelle hinzugefügt und ich möchte für Argumente Farbe: 1,5 Produkt mit ID 1 erhalten – Petr