MySQL 'user_id' in where 子句是不明确的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2788326/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:00:29  来源:igfitidea点击:

MySQL 'user_id' in where clause is ambiguous problem

mysqlmysql-error-1052

提问by HoMe

How can I correct the problem I keep getting from the code below which states 'user_id' in where clause is ambiguous. Thanks for the help in advance.

我怎样才能纠正我从下面的代码中得到的问题'user_id' in where clause is ambiguous。我在这里先向您的帮助表示感谢。

Here is the mysql table.

这是mysql表。

SELECT user.*, user_info.* 
FROM user 
INNER JOIN user_info ON user.user_id = user_info.user_id
WHERE user_id='$user_id'

回答by Daniel Vassallo

You simply need to specify which user_idto use, since both the user_infoand usertable have a field called user_id:

您只需要指定user_id要使用的对象,因为 theuser_infousertable 都有一个名为 的字段user_id

... WHERE user.user_id='$user_id'

SQL wouldn't tolerate this ambiguity, since for what it knows, both fields can represent totally different data.

SQL 不会容忍这种歧义,因为就它所知,这两个字段可以表示完全不同的数据。

回答by Dave Markle

The solution is to select each column explicitly everywhere. Don't use user.* and user_info.* in your select list, either:

解决方案是在任何地方明确选择每一列。不要在选择列表中使用 user.* 和 user_info.*,或者:

SELECT u.user_id, u.user_fudge, ui.foo, ui.bar
FROM user u
INNER JOIN user_info ui
    ON ui.user_id = u.user_id
WHERE u.user_id = '$user_id';

回答by user3773680

You should be mention alias name of your column and assign to user_id like

你应该提到你的列的别名并分配给 user_id 就像

SELECT user.*, user_info.* 
FROM user as u 
INNER JOIN user_info as ui ON u.user_id = ui.user_id
WHERE u.user_id='$user_id'

回答by Dee_wab

Try this.

尝试这个。

SELECT u.user_id, u.user_fudge, ui.foo, ui.bar
FROM user AS u
INNER JOIN user_info AS ui
   ON ui.user_id = u.user_id
WHERE u.user_id = '$user_id';