3

I have the following MySQL:

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

For some reason I'm getting

Column 'ID' in where clause is ambiguous

For complicated code reasons, I'm not renaming the alias as something else (such as ID2)
Shouldn't this work?

Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
Asaf
  • 8,106
  • 19
  • 66
  • 116

3 Answers3

8

You need to specify the table you are using for id in your the where clause. Otherwise the query will know not which one to use since you have the same column name in two tables

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

or if you want the bigcar ID you will have to use:

WHERE `bigcar`.`ID` ='1'; // not truck but bigcar which is the alias you created
Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
3

What about:

select  `car`.`ID` AS `ID`, 
    `title`,`text` 
    from `car`  
    LEFT JOIN `truck` as bigcar ON bigcar.`ID` = `car`.`truckID`  
    WHERE `car`.`ID` ='1'; 
Paddy
  • 33,309
  • 15
  • 79
  • 114
0

The error means the field ID exists in both tables. To fix it you should add a table alias to WHERE clause, e.g. - bigcar.ID = 1 or car.ID = 1 ...as you need.

Devart
  • 119,203
  • 23
  • 166
  • 186