sql >> Base de Datos >  >> RDS >> Mysql

Unirse a 2 tablas con identificación de clave externa

Podrías escribir algo como

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

Pero si tiene tablas realmente grandes, olvídese de la tercera forma normal y agregue nombres para categorías en la tabla de productos. Pero solo si tiene tablas realmente grandes con categorías.

UPD Agregar ORDER BY