简体   繁体   中英

Using multiple conditions in SQL WHERE clause

I have been searching online and even amongst Stackoverflow questions about this issue but couldnt receive any related response. The SQL query that I have shown below,

SELECT Scania.GensetType
    , Scania.EngineType
    , Scania.Engine60Hz
    , Scania.Alternator
    , Scania.PriceEur 
FROM Scania 
LEFT JOIN NordhavnGenset 
    ON Scania.GensetType=NordhavnGenset.Alternator 
WHERE (NordhavnGenset.MaxKva='46') 
    and (Generator.Alternator='ECP34-1LN/4') 
    and (insulation.Insulation='F (90ºC/45ºC)') 
    and (Klasse.Klasse='KRS\r') 
    and (airinletfilter.AirInletFilter='No') 
    and (Ip.IP='IP23');

Always returns the error message below, and I have checked several times that this column exists in the table!

#1054 - Unknown column 'Generator.Alternator' in 'where clause'

However if I execute the query by stopping at the first Where clause, then the results are displayed,

SELECT Scania.GensetType
    , Scania.EngineType
    , Scania.Engine60Hz
    , Scania.Alternator
    , Scania.PriceEur 
FROM Scania 
LEFT JOIN NordhavnGenset 
    ON Scania.GensetType=NordhavnGenset.Alternator 
WHERE (NordhavnGenset.MaxKva='46');

The new query

    SELECT  Scania.GensetType, Scania.EngineType, Scania.Engine60Hz, Scania.Alternator, Scania.PriceEur
FROM Scania  
LEFT JOIN NordhavnGenset
    ON Scania.GensetType=NordhavnGenset.Alternator 
LEFT JOIN Generator
    ON Scania.Alternator=Generator.Alternator
LEFT JOIN Insulation 
    ON NordhavnGenset.Insulation=Insulation.Insulation
LEFT JOIN Klasse
    ON NordhavnGenset.Class=Klasse.Class
LEFT JOIN AirInletFilter
    ON NordhavnGenset.AirInletFilter=AirInletFilter.AirInletFilter
LEFT JOIN IP
    ON NordhavnGenset.Ip=IP.IP
WHERE (NordhavnGenset.MaxKva='46') and (Generator.Alternator='ECP34-1LN/4') and (Insulation.Insulation='F (90ºC/45ºC)') and (Klasse.Klasse='KRS\r') and (AirInletFilter.AirInletFilter='No') and (IP.IP='IP23');

It does not appear that you are Joining on a table called Generator . You cannot have the table in your WHERE clause unless you JOIN on it.

If you add a JOIN to Generator then you can use it in the WHERE clause.

SELECT *
FROM Scania 
LEFT JOIN NordhavnGenset 
    ON Scania.GensetType=NordhavnGenset.Alternator 
LEFT JOIN Generator 
    ON ....
WHERE (Generator.Alternator='ECP34-1LN/4') ...

You also have other tables in your WHERE clause that are not JOINed on, so you will need to add those as well.

The reason for the error is that you are not joining the tables that you are using in the WHERE clause.

The following would always give an error -

and (Generator.Alternator='ECP34-1LN/4') 
and (insulation.Insulation='F (90ºC/45ºC)') 
and (Klasse.Klasse='KRS\r') 
and (airinletfilter.AirInletFilter='No') 
and (Ip.IP='IP23');

You either need to have the table joined or the alias of the table should be the name before the '.'.

You're not including the Generator table in the join. Either add it, or you may mean NordhavnGenset.Alternator instead of Generator.Alternator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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