All we need is an easy explanation of the problem, so here it is.
I’ve columns in different tables referenced in a one table, I’m running a query to fetch the fields related to those columns in their respective tables, but the query is returning only the last join (certstype.name from certifications.certid = certstype.id in this case) How do I modify the query to return names from all the tables I referenced. To make the question clear, I’ve also attached the relationship diagram.
Environment: PostGRESQL and PHP.
$query = "SELECT certifications.*, users.name, company.name, certstype.name FROM certifications INNER JOIN users ON certifications.candidate=users.id JOIN company ON certifications.company=company.id JOIN certstype ON certifications.certid=certstype.id WHERE certifications.candidate = 2 ORDER BY 5 ASC";
How to solve :
I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.
You are probably using a method to return data which needs unique column names, for example because it uses the column names as keys in a hash. You can add aliases to make the column names unique.
This is the first line of your query with aliases:
SELECT certifications.*, users.name as user_name, company.name as company_name, certstype.name as cert_type_name FROM ...
The "AS" keyword is optional between the name and the alias, but I think it makes things clearer.
If this doesn’t fix things for you, you should show us the php code which is executing the query and fetching the data.
Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂