简体   繁体   English

不喜欢无法在查询中排除结果SQL

[英]NOT LIKE failing to exclude results SQL in query

I'm using a WHERE clause to exclude certain records from my query results. 我正在使用WHERE子句从查询结果中排除某些记录。

where pv_table.docindex1 IS NULL AND
  pv_table.docindex2 IS NULL AND
  (data_file.vendornumber NOT IN (00550, 00510)
   OR data_file.invoicenumber NOT LIKE 'REB0000%')

This WHERE clause is successful in eliminating records with values 00550 and 00510 in the file.vendornumber column, but it is still including records that start with 'REB0000%' in the file.invoicenumber column in the query results. WHERE子句可以成功消除file.vendornumber列中值为00550和00510的记录,但仍包含查询结果中file.invoicenumber列中以'REB0000%'开头的记录。

Is there something wrong with my order of operations or my syntax using NOT LIKE ? 我的操作顺序或使用NOT LIKE语法有什么问题吗?

When using a WHERE NOT with multiple exclusion conditions, you need to use AND , not OR 当将WHERE NOT与多个排除条件一起使用时,您需要使用AND ,而不是OR

Example

ColumnA
1
2
3
4
5

select ColumnA
from MyTable
where ColumnA = 1
or ColumnA = 5

If I reverse this with where ColumnA <> 1 or ColumnA <> 5 , then 1 <> 5 and 5 <> 1 , so both appear in the results. 如果我用where ColumnA <> 1 or ColumnA <> 5颠倒了它,那么1 <> 55 <> 1 ,那么两者都出现在结果中。

So when we make a statement with a where not , we use AND to list the things we want to exclude 因此,当我们使用where not声明时,我们使用AND列出要排除的内容

You use OR in the condition, that is why you have records that start with 'REB0000%': 您在条件中使用OR ,这就是为什么您有以'REB0000%'开头的记录的原因:

data_file.vendornumber not in (00550, 00510) OR data_file.invoicenumber not like 'REB0000%'

If you want to exclude it you need to use AND instead of 如果要排除它,则需要使用AND而不是

Use 'AND' instead of 'OR' 使用“ AND”代替“ OR”

where pv_table.docindex1 IS NULL and pv_table.docindex2 IS NULL and 
  (data_file.vendornumber not in (00550, 00510) AND data_file.invoicenumber not like 'REB0000%')

I think you're getting your AND and OR mixed up. 我认为您将AND和OR混为一谈。

I'm thinking what you what is: 我在想你是什么:

  1. vendornumber not in this set AND 供应商编号不在此集合中,并且
  2. invoicenumber not like this 发票号码不是这样

There are two ways to accomplish this 有两种方法可以实现此目的

  1. change your OR to an AND, as multiple other people have suggested 正如其他人建议的那样,将您的OR更改为AND
  2. modify your where clause as follows: 修改您的where子句,如下所示:

    where pv_table.docindex1 IS NULL and pv_table.docindex2 IS NULL and not ( data_file.vendornumber in (00550, 00510) OR data_file.invoicenumber like 'REB0000%' ) 其中pv_table.docindex1为NULL而pv_table.docindex2为NULL(不是((00550,00510)中的data_file.vendornumber或'REB0000%'之类的data_file.invoicenumber))

Boolean algebra says that (NOT a AND NOT b) = NOT(a OR B). 布尔代数表示(NO a AND NOT b)= NOT(a OR B)。 Both of my suggestions should give you the same result. 我的两个建议都应该给您相同的结果。 Which one is easier to read ... debatable. 哪一个更容易阅读...值得商.。

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

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