简体   繁体   English

在SQL中合并2个Not Like语句

[英]Combining 2 Not Like Statements in SQL

I have a formula where I have a column for region and a column for district (a region is made up of districts), in the formula i have some pieces of code to exclude certain regions as a whole. 我有一个公式,其中有一个用于区域的列和一个用于区域的列(一个区域由区域组成),在公式中,我有一些代码段可以将某些区域整体排除在外。 But some i need to only exclude a few districts from some regions but not all. 但是有些我只需要从某些地区中排除少数地区,而不是全部。 The where portion of the code looks like this: In the code I want to exclude all of Region 100 and region 76, but for region 88 I only want to exclude District 04 but when i type the code like this it stil excludes all of 88. (There is no GROUP BY in this code) 代码的where部分如下所示:在代码中,我想排除所有区域100和区域76,但是对于区域88,我只想排除区域04,但是当我键入这样的代码时,stil会排除所有88 。(此代码中没有GROUP BY)

SELECT ID, Date, Class, Location, Training Number
FROM Table 1 INNER JOIN Table 2 ON Training Number     
WHERE (Region NOT LIKE '100') AND 
      (Region NOT LIKE '76') AND 
      (Region NOT LIKE '88') AND 
      (District NOT LIKE '04')

I am not sure why you are using the LIKE operator, you should be able to use: 我不确定为什么要使用LIKE运算符,应该可以使用:

SELECT ID, Date, Class, Location, Training Number
FROM Table 1 
INNER JOIN Table 2 
  ON Training Number     
WHERE Region <> '100' 
  AND Region <> '76'
  AND 
  (
    Region <> '88' 
    OR District <> '04'
  );

Or you can use NOT IN : 或者您可以使用NOT IN

SELECT ID, Date, Class, Location, Training Number
FROM Table 1 
INNER JOIN Table 2 
  ON Training Number     
WHERE 
(
  Region NOT IN ('100', '76')
)
AND 
(
  Region <> '88' 
  OR District <> '04'
)

See SQL Fiddle with Demo 参见带有演示的SQL Fiddle

How about something like: 怎么样:

SELECT ID, Date, Class, Location, Training Number
FROM Table 1 INNER JOIN Table 2 ON Training Number     
WHERE (Region <> '100') AND -- Region cannot be 100
      (Region <> '76') AND  -- Region cannot be 76
      (Region <> '88' OR District <> '04') -- Region cannot be 88 if District is 04

Just do this: 只要这样做:

where Region not in ('100', '76', '88') and district <> '04'

You don't need to use like for this. 你并不需要使用like这一点。

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

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