简体   繁体   中英

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

What should I do is it wrong to name the OrganisationID in tbl_user same as tbl_organisation. OrganisationID is a foreign key from 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.

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". Otherwise the engine doesn't know which OrganisationID you meant.

It is not wrong the have the same column names in two tables. In many (even most) cases, it is actually perferred.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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