Possible Duplicate:
How to LEFT JOIN two tables with the same column name
I have this statement:
$sql = "SELECT * FROM deliveries AS del
LEFT JOIN job_sheet ON job_sheet.job_id=del.jID
LEFT JOIN hierarchy AS h ON job_sheet.h_str=h.hID
LEFT JOIN customers ON h.cust=customers.ID";
My problem is that the ‘customers’ table has a column called ID and so does the ‘deliveries’ table. The issue starts when I run a while loop to output results.
while($data = mysql_fetch_array($sql))
{
echo $data['ID'];
}
it outputs the value of the last table that was joined… How can I separate the two? I mean, I need to be able to output ‘ID’ from both tables, but in the query, it’s the last table that wins. Renaming the column is not an option…
Any inputs?
I figured it out with some tweaking:
Given that the only thing I needed from the customers table was an aliased ‘ID’ while keeping every other column from the other two tables.
My while loop would ultimately change to: