SQL - AND OR组合

时间:2017-12-20 12:20:14

标签: sql arcgis

此查询有效

SELECT *
FROM ALL
WHERE Date >= '2017-04-04 00:00:00'
AND
CONFIDENCE <> 'Discarded'
AND 
CONTEXT <>'Home'

我希望能够说: 并(CONTEXT <> 'Home') OR (CONTEXT = 'Hospital' AND LOCAL = 1)

这样我就可以选择所有记录了 从2017年4月起,不得丢弃,其背景不等于家庭

OR

从2017年4月开始,不要丢弃,或者上下文=医院和本地= 1。

我正在使用ArcGIS。 我试过这个:

"DATE_ENTER"  >= date '2017-04-04 00:00:00'
AND
"CONFIDENCE" <> 'Discarded'
AND 
(
("CONTEXT" <> 'Home address') OR ("CONTEXT" = 'Hospital' AND "LOCAL" = 1)
)

但仍然有Context = Hospital和Local = 0的记录

当我测试时

 ("CONTEXT" <> 'Home address') 

("CONTEXT" = 'Hospital' AND "LOCAL" = 1)
单独,它有效,我很难将两者结合起来给我我需要的东西。

我看了一眼 SQL AND OR query

第一

4 个答案:

答案 0 :(得分:0)

我认为你真的想要:

WHERE Date >= '2017-04-04' AND  -- time component is unnecessary
      CONFIDENCE <> 'Discarded' AND 
      CONTEXT <> 'Home' AND
      (NOT (CONTEXT = 'Hospital' AND LOCAL = 1) )

CONTEXT = 'Hospital'时,它不等于'Home'。因此,它仍然符合CONTEXT上的条件。

答案 1 :(得分:0)

我认为情况应该是这样的:

<Book>
  <Title>Book1</Title>
  <Type>fiction</Type>
  <Cost>9.99</Cost>
</Book>

<Book>
  <Title>Book2</Title>
  <Type>non-fiction</Type>
  <Cost>5.99</Cost>
</Book>

<Book>
  <Title>Book3</Title>
  <Type>fiction</Type>
  <Cost>14.99</Cost>
</Book>

<Book>
  <Title>Book4</Title>
  <Type>non-fiction</Type>
  <Cost>19.99</Cost>
</Book>
...

当您使用... AND ( ("CONTEXT" <> 'Home address' AND "CONTEXT" <> 'Hospital') OR ("CONTEXT" = 'Hospital' AND "LOCAL" = 1) ) 时,由于第一个条件("CONTEXT" <> 'Home address') OR ("CONTEXT" = 'Hospital' AND "LOCAL" = 1)

,您获得的结果But there are still records with Context = Hospital and Local = 0是正确的

答案 2 :(得分:0)

(CONTEXT NOT IN('Hospital' , 'Home') OR (CONTEXT = 'Hospital' AND LOCAL= 1))

答案 3 :(得分:0)

WHERE Date >= '2017-04-04' AND  -- time component is unnecessary
  CONFIDENCE <> 'Discarded' AND 
  CONTEXT <> 'Home' AND NOT(CONTEXT = 'Hospital' AND LOCAL = 0) 

作品

一样
    WHERE Date >= '2017-04-04' AND  -- time component is unnecessary
              CONFIDENCE <> 'Discarded' 
AND
    (
       ("CONTEXT" <> 'Home address' AND "CONTEXT" <> 'Hospital') 
       OR ("CONTEXT" = 'Hospital' AND "LOCAL" = 1)
    )