即使使用Alias,MySQL中的模糊列也是如此

时间:2011-11-25 14:27:33

标签: mysql ambiguous

我有以下MySQL:

select  `car`.`ID` AS `ID`,
    `title`,`text`
    from `car` 
    LEFT JOIN `truck` as bigcar ON bigcar.`ID` = `car`.`truckID` 
    WHERE `ID` ='1';

由于某种原因,我正在

  

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

出于复杂的代码原因,我不会将别名重命名为其他内容(例如ID2)
这不应该起作用吗?

3 个答案:

答案 0 :(得分:8)

您需要在where子句中指定用于id的表。否则,查询将不知道要使用哪一个,因为在两个表中具有相同的列名

SELECT
 `car`.`ID` AS `ID`, `title`,`text`
FROM `car` 
LEFT JOIN `truck` as bigcar ON bigcar.`ID` = `car`.`truckID` 
WHERE `car`.`ID` ='1';

或者如果您需要bigcar ID,则必须使用:

WHERE `bigcar`.`ID` ='1'; // not truck but bigcar which is the alias you created

答案 1 :(得分:3)

怎么样:

select  `car`.`ID` AS `ID`, 
    `title`,`text` 
    from `car`  
    LEFT JOIN `truck` as bigcar ON bigcar.`ID` = `car`.`truckID`  
    WHERE `car`.`ID` ='1'; 

答案 2 :(得分:0)

错误表示两个表中都存在字段ID。要修复它,您应该为WHERE子句添加一个表别名,例如: - bigcar.ID = 1或car.ID = 1 ......根据需要。