简体   繁体   English

字段列表中的“user_id”列不明确

[英]Column 'user_id' in field list is ambiguous

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?这是什么意思?

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

column user_id is in both table_reviews , table_users tables.列user_id是在两个table_reviewstable_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

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

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.不仅在 where 子句(WHERE us.user_id = 1)中检查选定的值(SELECT username、image、user_id),在 where 子句中检查所有连接的列,因此在您的示例中,两个连接表中都有 user_id 列。 If you have query like this one:如果您有这样的查询:

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。

This is because user_id field exists in both tables.这是因为两个表中都存在user_id字段。 You need to specify which table's user_id you want.您需要指定您想要哪个表的user_id

SELECT username, image, re.user_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM