排除验证两个条件的记录

问题描述 投票:2回答:3
select * from table_name
 where BaseDocEntry=15 and BaseDocType='PRRH' and ItemCode='TestStudy2'
   and WhseCode='01' and (currentdoctype<>'PUSH' and CurrentDocEntry<>15)

根据我上面写的查询,来自INS2的数据将被取出,不包括currentdoctype [PUSH]和CurrentDocEntry不是15的所有数据。我期望我的查询是,它必须排除数据,只有这个组合,即currentdoctype = PUSH和CurrentDocEntry = 15发生在同一行,然后排除该行。

你能解决这个问题吗?

sql sql-server select
3个回答
13
投票

用这个:

and not (currentdoctype='PUSH' and CurrentDocEntry=15)

1
投票

由于您使用的是AND而不是OR,因此当currentdoctype与'PUSH'不同且CurrentDocEntry与15不同时,查询将被排除。

如果你想要currentdoctype = PUSH和CurrentDocEntry = 15,那就改为:

select * from ins2
where BaseDocEntry=15 and BaseDocType='PRRH' and ItemCode='TestStudy2'
and WhseCode='01' and (currentdoctype='PUSH' and CurrentDocEntry=15)

0
投票

在NOT不起作用的某些情况下,您可以使用简单的case和where子句来解决它。这将仅排除currentdoctype ='PUSH'和CurrentDocEntry = 15的情况。

SELECT *,
CASE
WHEN currentdoctype ='PUSH' AND CurrentDocEntry = 15 THEN 'c1'
ELSE 'c2'
END AS com_con
FROM table_name
WHERE  BaseDocEntry=15 AND BaseDocType='PRRH' AND ItemCode='TestStudy2'
   AND WhseCode='01' AND com_con = 'c2';
© www.soinside.com 2019 - 2024. All rights reserved.