Sunday, 15 September 2013

MySQL join same table twice on different columns without a clash

MySQL join same table twice on different columns without a clash

This is the query:
SELECT * FROM property_table AS property
INNER JOIN property_classification AS classifications
ON property.classification_id = classifications.id
INNER JOIN property_classification AS classonrequest
ON property.classonrequest_id = classonrequest.id
WHERE property.id=5000 LIMIT 1;
Notice that I'm using the same table property_classification on two fields
property.classification_id and property.classonrequest_id.
The structure of property_classification is something like:
id | a1 | a2 | a3 | ... | d1 | d2
When I execute the query above in MySQL Query Browser, I get something
like this:
id | other 'property' fields | id | a1 | a2 | a3 | ... | id | a1 | a2 | a3
| ...
But in my PHP script I am returning associated arrays, and all duplicate
field names are overwritten.
What I want is the query to return the two joined tables under the name of
their table i.e.:
classifications.id | classifications.a1 | classifications.a2 |
classifications.a3
and
classonrequest.id | classonrequest.a1 | classonrequest.a2 | classonrequest.a3
How do I do that?

No comments:

Post a Comment