Skip to content
Advertisement

How avoid ambiguousness when joining tables with a where statement?

i have a query such as

SELECT * 
FROM bookings  
left JOIN answers on bookings.id=answers.booking_id 
where id=1

since both tables have id and booking_id i am getting : column reference "id" is ambiguous

Advertisement

Answer

Qualify the column references — all of them.

To facilitate this, use table alias that are abbreviations of the table names. Something like this:

SELECT * 
FROM bookings b LEFT JOIN
     answers a
     ON b.id = a.booking_id 
WHERE b.id = 1;

(Note: I don’t know if you want b.id = 1 or a.id = 1, but the latter condition should be in the ON clause for a LEFT JOIN.)

I would also advise you to list out the columns in the SELECT — you have duplicate names so you want to be careful there too.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement