I have a SQL query with a left join which works fine:
SELECT book.* FROM book
LEFT JOIN purchase ON book.book_id = purchase.book_id
WHERE purchase.user_id = 3
ORDER BY purchase.purchase_date
But I need also infos from purchase table, so I tried:
SELECT purchase.*, book.*
FROM purchase, book
LEFT JOIN purchase ON book.book_id = purchase.book_id
WHERE purchase.user_id = 3
ORDER BY purchase.purchase_date
But it does not work, I have this error message: #1066 – Not unique table/alias: ‘purchase’
How can I do modify the first request to get data from purchase table also ?
Your 1st statement was nearly exactly what you want, you just need to name in the SELECT, which fields you want to return from purchase table.
e.g.
You don’t need to list “purchase” in the FROM clause as well as in the JOIN – because you have, that is why you are seeing the error.