Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column 'user_id' in field list is ambiguous

Tags:

sql

mysql

I am attempting to get the user information of the user who left this review.

With the following code:

SELECT username, image, user_id FROM table_users AS us
JOIN table_reviews AS re
ON re.user_id = us.user_id
WHERE us.user_id = 1 AND
re.review_id= 1

I get the error:

Column 'user_id' in field list is ambiguous

What does this mean?

like image 666
crmepham Avatar asked Sep 28 '13 16:09

crmepham


People also ask

How can you fix an ambiguous column reference error?

You may see an error that says something like Column 'id' in field list is ambiguous . This error means that there is a field name that is present in more than one table, so it needs to be scoped with the table name to avoid ambiguity: using orders.id instead of just id will resolve the issue.

How do you resolve ambiguous column names?

This means two columns have the same column name — that is the “Name” column. The SQL Machine is confused as to which “Name” out of the two tables you are referring to. It is ambiguous — not clear. To clarify this, add the alias of either or both TABLE1 or TABLE2 to the columns having the same name.

How do I fix SQL error 1052?

Fixing the error To fix this, simply add the tablename or alias for the table you want to work with. If you are writing the query yourself, this is a bit easier to deal with as you will know which table you meant to use. In our example, we should add the alias for the oc_customer table, c, to the column names.

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.


3 Answers

It means that both tables in the query have the column user_id.

You need to specify which one you want to get in the SELECT statement like SELECT username, image, re.user_id

like image 136
MikeSmithDev Avatar answered Oct 13 '22 06:10

MikeSmithDev


column user_id is in both table_reviews, table_users tables.

You need to specify columns with table alias name also.

SELECT username, image, us.user_id FROM table_users AS us
JOIN table_reviews AS re
ON re.user_id = us.user_id
WHERE us.user_id = 1 AND
re.review_id= 1
like image 35
Praveen Prasannan Avatar answered Oct 13 '22 08:10

Praveen Prasannan


It means column user_id is in both tables ie, in table_reviews, table_users

Try like this:

SELECT username, image, us.user_id    //or re.user_id
FROM table_users AS us
JOIN table_reviews AS re
ON re.user_id = us.user_id
WHERE us.user_id = 1 AND
re.review_id= 1
like image 21
Rahul Tripathi Avatar answered Oct 13 '22 06:10

Rahul Tripathi