我的数据库中有一个名为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')
4条答案
按热度按时间vvppvyoh1#
您可以在以下链接查看此查询:
http://sqlfiddle.com/#!18/11b3d/2
rjee0c152#
您有一个状态列表(xxx、ina、nfc),您不希望记录具有这些状态。此外,当帐号为空时,您只希望记录具有RWD状态,这意味着您需要将该状态添加到您不希望的状态列表中。这将为您提供如下查询:
bfrts1fy3#
问题是
status not in ('xxx','ina','nfc')
允许结果包含任何status
='rwd',即使account#
不为空。这使得(status='rwd' and account# is null)
冗余。您需要在status not in
查询中包含'rwd'。a11xaf1n4#
试试这个