I have 3 separate tables: ‘object’ (id, name, model_id), ‘object_model’ (id, name, type_id) and ‘object_type’ (id, name).
How can I properly join these three to get the appropriate names from all the tables?
I’m trying to join by doing the following:
SELECT object.id AS id, object.name AS name, object_model.name AS model_name, object_type.name AS type_name
FROM object
LEFT JOIN object_model ON object_model.id = object.model_id
LEFT JOIN object_type ON object_type.id = object.model_type_id
But get the error:
“Unknown column ‘object.model_type_id’ in ‘on clause'”
Your column is called
object_model.type_id. You have the dot and underscore in the wrong places.Otherwise, your query looks like it will do what you want it to do.