在SQL Server的where子句中使用多个条件

在SQL Server的where子句中使用多个条件,sql,sql-server,operators,where-clause,Sql,Sql Server,Operators,Where Clause,我的数据库中有一个名为finalres的表,其中有状态和帐户列表 我想提取状态不应为的帐户: (xxx, ina, nfc) 此外,我想拉帐户的状态在RWD,但只有当帐户是空的。我写了下面的查询,但它只给出了一个条件的结果。请帮帮我 select * from finalres where 1 = 0 or (status = 'rwd' and account# is null) or status not in ('xxx', 'ina', 'nfc') 您可以通过以下链接检

我的数据库中有一个名为finalres的表,其中有状态和帐户列表

我想提取状态不应为的帐户:

(xxx, ina, nfc)
此外,我想拉帐户的状态在RWD,但只有当帐户是空的。我写了下面的查询,但它只给出了一个条件的结果。请帮帮我

select *
from finalres
where 1 = 0
   or (status = 'rwd' and account# is null)
   or status not in ('xxx', 'ina', 'nfc')
您可以通过以下链接检查此查询:

试试这个

    select * 
      from finalres 
     where (status='rwd' and account# is null) 
        or status not in ('xxx','ina','nfc')

您有您不希望记录的状态列表xxx、ina、nfc。此外,当帐户为空时,您只需要状态为RWD的记录,这意味着您需要将该状态添加到您不想要的状态列表中。这将为您提供如下查询:

select
    *
from
    finalres
where
     status not in ('rwd','xxx','ina','nfc')
  or (status='rwd' and account is null)
问题是状态不在'xxx','ina','nfc'允许结果包括任何状态='rwd',即使帐户不为空。这使得status='rwd'和account为空冗余。您需要将“rwd”包含在“不在查询中”状态中


它仍然不起作用。它只起第一个作用condition@unnikrishnan请将表格中的一些数据放入表格中。请编辑您的问题,以显示源数据和查询结果的示例数据,从而说明您希望发生的事情以及当前查询的错误。where子句中的1=0是什么意思?@Farshad So开发人员可以在和/或轻松地添加条件,而无需花费时间来查找在何处添加where语句。这只是为了方便。如果状态为'rwd',则状态不在'xxx','ina','nfc'中是多余的。这就像说x=4,x不在1,2,3,5,6,7中,你已经知道x是4,所以它不能是任何其他的值。x和Y和Z在功能上与x和Y相同,Z在功能上与x和Y相同,Z在功能上与x和Z和Y相同,等等,等等。
    select * 
      from finalres 
     where (status='rwd' and account# is null) 
        or status not in ('xxx','ina','nfc')
select
    *
from
    finalres
where
     status not in ('rwd','xxx','ina','nfc')
  or (status='rwd' and account is null)
select 
*
from finalres
where 1 = 0
or (status='rwd' and account# is null)
or status not in ('rwd','xxx','ina','nfc')