1

我有一个公式,其中我有一个区域列和一个区域列(一个区域由区域组成),在公式中我有一些代码可以将某些区域作为一个整体排除。但有些我只需要排除某些地区的几个地区,但不是全部。代码的 where 部分如下所示:在代码中,我想排除所有 100 区和 76 区,但对于 88 区,我只想排除 04 区,但是当我输入这样的代码时,它仍然排除了所有 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')
4

3 回答 3

5

我不确定您为什么使用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'
  );

或者您可以使用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'
)

请参阅带有演示的 SQL Fiddle

于 2013-01-18T20:04:21.430 回答
1

怎么样:

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
于 2013-01-18T20:06:14.177 回答
0

只需这样做:

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

你不需要为此使用like

于 2013-01-18T20:04:16.960 回答