我有以下MySQL:
select `car`.`ID` AS `ID`,
`title`,`text`
from `car`
LEFT JOIN `truck` as bigcar ON bigcar.`ID` = `car`.`truckID`
WHERE `ID` ='1';
出于某种原因,我得到了
Column ‘ID’ in where clause is ambiguous
出于复杂的代码原因,我不会将别名重命名为其他内容(例如ID2)
这不应该工作吗?
解决方法:
您需要在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