Search code examples
sqlclause

#1054 - Unknown column 'Klanten.Klantnummer' in 'on clause'


I know that I have seen a couple of other questions about this error but I'm new to the sql JOIN so plz could you guy explain what I'm doing wrong.

Here's my query

SELECT Klanten.Klantnummer,`Barcode`, `Naam`, `BetaalStatus`, `ScanStatus`, `TijdScan`, `Prijs` 
FROM `Klanten`, `kaart` 
LEFT JOIN (`Intro`) 
ON (Intro.KlantNummer = Klanten.Klantnummer) 
WHERE kaart.KlantNummer = Klanten.Klantnummer

This is the Error I get like you have seen in the title

1054 - Unknown column 'Klanten.Klantnummer' in 'on clause'

And the db names are correct


Solution

  • Simple rule: Never use commas in the FROM clause. Always use proper, explicit JOIN syntax. If you did that, you would not have an error:

    SELECT Klanten.Klantnummer,`Barcode`, `Naam`, `BetaalStatus`, `ScanStatus`, `TijdScan`, `Prijs` 
    FROM `Klanten` JOIN
         `kaart`
         ON kaart.KlantNummer = Klanten.Klantnummer LEFT JOIN 
         `Intro`
         ON Intro.KlantNummer = Klanten.Klantnummer ;
    

    The problem is that the precedence of , and JOIN are different. Hence, the table before the comma is not known to the ON clause.