Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous column name

Tags:

sql

I have the following SQL and it throws the error Ambiguous column name 'id'

select tbl_registration.*, tbl_ebp.name as ebp_name, tbl_Users.id as user_id, tbl_ebp.id as linked_ebp_id
from tbl_registration
left outer join tbl_ebp on tbl_ebp.id = tbl_registration.ebp_id
left outer join tbl_users on tbl_registration.email = tbl_users.username
where id = [PARAM]p_id

I've read some articles on this, but can't find a working solution for my code. Any help much appreciated.

like image 582
Cameron Avatar asked Jul 12 '10 16:07

Cameron


People also ask

What is ambiguous error?

Ambiguity errors occur when erasure causes two seemingly distinct generic declarations to resolve to the same erased type, causing a conflict.

What does ambiguous error mean in SQL?

Ambiguous error means that you are calling a certain field in which exist in both Table and the SQL has no idea where to get it.


1 Answers

Your WHERE clause id needs to be more specific, include the table name:

WHERE table.id = [PARAM]p_id

If two things share the same name, this is where the ambiguity steps in. In this case multiple tables in your SQL contain the "id" column.

SQL has the intelligence to disambiguate column names if the column name is unique across the current set of tables being touched - hence most of the time you don't need to prefix column names with table names.

like image 119
Adam Houldsworth Avatar answered Oct 15 '22 22:10

Adam Houldsworth