Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Verbinden von 2 Tabellen mit Fremdschlüssel-ID

Sie könnten so etwas schreiben wie

SELECT product.*, bottom_category.name, top_category.name
FROM product
LEFT JOIN bottom_category ON bottom_category.id = product.bottom_category_id 
LEFT JOIN top_category ON top_category.id = bottom_category.top_category_id
ORDER BY top_category.id,bottom_category.id

Aber wenn Sie wirklich große Tabellen haben, vergessen Sie einfach die 3. Normalform und fügen Sie Namen für Kategorien in die Produkttabelle ein. Aber nur wenn Sie wirklich große Tabellen mit Kategorien haben.

UPD Fügen Sie ORDER BY hinzu