简体   繁体   English

MySQL中的where子句中的列''不明确

[英]Column ' ' in where clause is ambiguous Error in mysql

SELECT tbl_user.userid,
       tbl_user.firstname,
       tbl_user.lastname,
       tbl_user.email,
       tbl_user.created,
       tbl_user.createdby,
       tbl_organisation.organisationname
FROM   tbl_user
       INNER JOIN tbl_organisation
         ON tbl_user.organisationid = tbl_organisation.organisationid
WHERE  organisationid = @OrganisationID;  

I am using this statement to do a databind. 我正在使用此语句执行数据绑定。 I am getting a error here. 我在这里出错。

Column 'OrganisationID' in where clause is ambiguous where子句不明确的“ OrganisationID”列

What should I do is it wrong to name the OrganisationID in tbl_user same as tbl_organisation. 我该怎么办,在tbl_user中将OrganisationID命名为与tbl_organisation相同是错误的。 OrganisationID is a foreign key from tbl_Organisation OrganisationID是tbl_Organisation的外键

Since you have two columns with the same name on two different tables (and that's not a problem, it's even recommended on many cases), you must inform MySQL which one you want to filter by. 由于在两个不同的表上有两个具有相同名称的列(这不是问题,在许多情况下甚至建议这样做),因此必须通知MySQL您要作为过滤依据。

Add the table name (or alias, if you were using table aliases) before the column name. 在列名之前添加表名(如果使用表别名,则添加别名)。 In your case, either 就您而言,

WHERE tbl_user.OrganisationID

or 要么

WHERE tbl_Organisation.OrganisationID

should work. 应该管用。

You just need to indicate which table you are targeting with that statement, like "tbl_user.OrganisationID". 您只需要使用该语句指出要定位的表,例如“ tbl_user.OrganisationID”。 Otherwise the engine doesn't know which OrganisationID you meant. 否则,引擎将不知道您的意思是哪个OrganisationID。

It is not wrong the have the same column names in two tables. 在两个表中具有相同的列名是没有错的。 In many (even most) cases, it is actually perferred. 在很多(甚至大多数)情况下,实际上都是这样。

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

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