简体   繁体   English

在SQL WHERE子句中使用多个条件

[英]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. 我一直在网上搜索,甚至在Stackoverflow有关此问题的问题中搜索,但无法收到任何相关的答复。 The SQL query that I have shown below, 我在下面显示的SQL查询,

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, 但是,如果我通过在第一个Where子句处停止执行查询,则会显示结果,

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 . 似乎没有在名为Generator的表上加入。 You cannot have the table in your WHERE clause unless you JOIN on it. 除非在表上进行JOIN ,否则无法在WHERE子句中使用该表。

If you add a JOIN to Generator then you can use it in the WHERE clause. 如果将JOIN添加到Generator则可以在WHERE子句中使用它。

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. 您的WHERE子句中还有其他未JOINed表,因此您也需要添加这些表。

The reason for the error is that you are not joining the tables that you are using in the WHERE clause. 该错误的原因是您没有联接WHERE子句中使用的表。

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. 您没有在Generator中包括Generator表。 Either add it, or you may mean NordhavnGenset.Alternator instead of Generator.Alternator 要么添加它,要么意味着NordhavnGenset.Alternator而不是Generator.Alternator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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