简体   繁体   English

mysql查询模棱两可的错误

[英]mysql query ambiguous error

I am getting the error when i am trying to query this sql. 当我尝试查询此sql时出现错误。

#1052 - Column 'charge_id' in where clause is ambiguous

Below is my query. 以下是我的查询。

SELECT `inmate`.`inmate_id`,`inmate`.`fname`,`inmate`.`lname`,`inmate_case`.*,`case_information`.`case_id`,`case_charge`.*,`charge`.`charge_id` FROM inmate , `charge`
LEFT JOIN `prison`.`inmate_case` ON `inmate`.`inmate_id` = `inmate_case`.`inmate_id` 
LEFT JOIN `prison`.`case_information` ON `inmate_case`.`case_id` = `case_information`.`case_id` 
LEFT JOIN `prison`.`case_charge` ON `case_information`.`case_id` = `case_charge`.`case_id` 
 WHERE(( charge_id = 3)) 

You must have multiple tables with a column named charge_id . 您必须有多个表,该表的列名为charge_id Since you're putting it in your result set, you can change your WHERE clause to 由于您将其放入结果集中,因此可以将WHERE子句更改为

WHERE charge.charge_id = 3

If you only need the first and last name then you can drop some of the columns from your select - you must be pulling more than one column named charge_id (probably from case_charge ). 如果只需要名字和姓氏,则可以从选择中删除某些列-您必须提取多个名为charge_id列(可能是case_charge )。 You can simplify the SELECT to something like: 您可以将SELECT简化为:

SELECT inmate.inmate_id, inmate.fname, inmate.lname
FROM inmate
INNER JOIN prison.inmate_case ON inmate.inmate_id = inmate_case.inmate_id 
INNER JOIN prison.case_information ON inmate_case.case_id = case_information.case_id 
INNER JOIN prison.case_charge ON case_information.case_id = case_charge.case_id
INNER JOIN charge ON case_charge.charge_id = charge.charge_id
 WHERE charge.charge_id = 3

and add columns to your SELECT explicitly, as needed. 并根据需要将列显式添加到SELECT中。 I changed your LEFT JOIN s to INNER JOIN s since you only want inmates that do appear in the charge table. 我将您的LEFT JOIN更改为INNER JOIN因为您只希望确实在charge表中显示的犯人。 You would use OUTER JOIN if you're trying to pull inmates that MAY or MAY NOT appear in the other tables. 如果您要拉出可能在其他表中出现或可能不出现的犯人,则可以使用OUTER JOIN (Although they would still work in this case since you're putting the end field in the WHERE clause. Without that you would return all inmates, even if they have no cases or charges at all). (尽管在这种情况下他们仍然可以使用,因为您将end字段放在WHERE子句中。否则,即使他们没有任何案件或指控,您也将返回所有囚犯)。

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

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