mysql - How canI join a table with itself for a query in a PHP script, and tell the script which column to pick from which table?

552

Solution:

You actually have table aliases (c and cc). So you need aliases for your attributes. I mean something like this:

$sql = "SELECT DISTINCT c.co_name as nameA, cc.co_name as nameB
        FROM Deliveries d 
            INNER JOIN Companies c ON d.id_co = c.id_co, 
                                Deliveries dd 
            INNER JOIN Companies cc ON dd.id_co = cc.id_co

        WHERE d.id_obj = dd.id_obj 
        AND d.id_sup = dd.id_sup 
        AND d.id_co < dd.id_co";

Later use your echo line like this:

echo "<tr><td>".$row["nameA"]."</td><td>".$row["nameB"]."</td></tr>";

Hope it helps you...

People are also looking for solutions to the problem: pthreads - How to Install PHP-Zts manually on the Centos 7

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.