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?
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.
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.
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.
Ambiguity errors occur when erasure causes two seemingly distinct generic declarations to resolve to the same erased type, causing a conflict.
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With