Q:

SQL JOINS and naming joined column

I have a Products table and Downloads table.
The Downloads table has 4 fields, ID, Name, Category and Download
The Products table has 3 fields specific to Downloads: Downloads, Order Guides and Submittal Sheets.  Each one of these fields stores the ID of the record from the Downloads table.  There will never be the same Download ID value for these 3 fields in the Product table.
I have the follow SQL statement:
SELECT product_id, product_name, product_download, product_submittal, product_ordering_guide, product_status, tbl_downloads.download_id, tbl_downloads.download_name
FROM tbl_products
LEFT JOIN tbl_downloads ON tbl_products.product_download=tbl_downloads.download_id
LEFT JOIN tbl_downloads ON tbl_products.product_submittal=tbl_downloads.download_id
LEFT JOIN tbl_downloads ON tbl_products.product_order_guide=tbl_downloads.download_id

It generates the following error:

#1066 - Not unique table/alias: 'tbl_downloads'

This error makes sense and I know it was going to happen, but I don't know how to fix it.  I need to add an Alias, but not sure where.
If I remove the last two JOIN statements, everything works as expected.
Thanks

A:

You need to use unique aliases if you are joining the same table multiple times:
SELECT product_id, product_name, 
    product_download, product_submittal, 
    product_ordering_guide, product_status, 
    d1.download_id DownloadId, 
    d1.download_name DownloadName,
    d2.download_id SubmittalDownloadId, 
    d2.download_name SubmittalDownloadName,
    d3.download_id GuideDownloadId, 
    d3.download_name GuideDownloadName
FROM tbl_products
LEFT JOIN tbl_downloads d1
    ON tbl_products.product_download=d1.download_id
LEFT JOIN tbl_downloads d2
    ON tbl_products.product_submittal=d2.download_id
LEFT JOIN tbl_downloads d3
    ON tbl_products.product_order_guide=d3.download_id

For example I used d1, d2 and d3 but you might want to be more descriptive in your aliases so it is clear what each join is doing, like this:
SELECT product_id, product_name, 
    product_download, product_submittal, 
    product_ordering_guide, product_status, 
    download.download_id DownloadId, 
    download.download_name DownloadName,
    submittal.download_id SubmittalDownloadId, 
    submittal.download_name SubmittalDownloadName,
    guide.download_id GuideDownloadId, 
    guide.download_name GuideDownloadName
FROM tbl_products
LEFT JOIN tbl_downloads download
    ON tbl_products.product_download=download.download_id
LEFT JOIN tbl_downloads submittal
    ON tbl_products.product_submittal=submittal.download_id
LEFT JOIN tbl_downloads guide
    ON tbl_products.product_order_guide=guide.download_id

