MySQL 字段列表中的“user_id”列不明确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19068954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Column 'user_id' in field list is ambiguous
提问by crmepham
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
字段列表中的“user_id”列不明确
What does this mean?
这是什么意思?
回答by MikeSmithDev
It means that both tables in the query have the column user_id
.
这意味着查询中的两个表都有列user_id
。
You need to specify which one you want to get in the SELECT statement like
SELECT username, image, re.user_id
您需要在 SELECT 语句中指定要获取的内容,例如
SELECT username, image, re.user_id
回答by Praveen Prasannan
column user_id is in both table_reviews
, table_users
tables.
列user_id是在两个table_reviews
,table_users
表中。
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
回答by Rahul Tripathi
It means column user_id is in both tables ie, in table_reviews, table_users
这意味着列 user_id 在两个表中,即在 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
回答by fico7489
Not only selected values(SELECT username, image, user_id) are examined in where clause (WHERE us.user_id = 1), all joined columns are examined in where clause, so in your example you have user_id column in both joined tables. If you have query like this one:
不仅在 where 子句(WHERE us.user_id = 1)中检查选定的值(SELECT username、image、user_id),在 where 子句中检查所有连接的列,因此在您的示例中,两个连接表中都有 user_id 列。如果您有这样的查询:
SELECT table_reviews.id FROM table_users AS us
JOIN table_reviews AS re
ON re.user_id = us.user_id
WHERE id
id will be ambiguous because id-s from table_reviews and table_reviews will be examined in where clause although only id from table_reviews is selected.
id 将是不明确的,因为来自 table_reviews 和 table_reviews 的 id-s 将在 where 子句中进行检查,尽管只选择了来自 table_reviews 的 id。