Multiple JOINs returning only the last referenced table

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 ( from certifications.certid = 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.

Multiple JOINs returning only the last referenced table

    $query = "SELECT certifications.*,,,
                    FROM certifications
                    INNER JOIN users
                    JOIN company
                    JOIN certstype
            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.

Method 1

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.*, as user_name, as company_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 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply