输出中的结果不相关?

时间:2016-08-28 13:18:59

标签: sql sql-server-2012

这是我的疑问:

SELECT TOP 10000 [Service_order]
      ,[COMPANY]
      ,[PENDING_DAYS]
      ,[SERVICE_TYPE]
      ,[SERVICE_TYPE_TXT]
      ,[STATUS]
      ,[STATUS_TEXT]
      ,[REASON]
      ,[REASON_TEXT]
      ,[ASC code]
      ,[ASC name]
      ,[MODEL]
      ,[INOUTWTY]
      ,[Part_code1]
      ,[PS1]
      ,[confirmation_No1]
      ,[Part_code2]
      ,[PS2]
      ,[SO_NO2]
      ,[Part_code3]
      ,[PS3]
      ,[SO_NO3]
      ,[Part_code4]
      ,[PS4]
      ,[SO_NO4]
      ,[Part_code5]
      ,[PS5]
      ,[SO_NO5]
      ,[Engineer name]
  FROM ['NewLP']
where REASON_TEXT = 'Parts Not available (ASC)'
or  REASON_TEXT = 'Parts In Transit (Samsung)'
or REASON_TEXT = 'Parts Back Ordered (Samsung)'
  and PS1 = 'U' 
  and PS2 = 'U' or PS2 = ''
  and PS3 = 'U' or PS3 = ''
  and PS4 = 'U' or PS4 = ''
  and PS5 = 'U' or PS5 = ''

我不知道究竟哪个部分是错误的,但是reason_text应该是条件中的那些,并且首先ps应该是' U'其余的应该是' U'或者为空,但是当我运行查询时,结果不是它应该是什么。 结果如下:

enter image description here

当我在条件中使用其中一个reason_text时,只有前两个PS正确显示,其余的(PS3,PS4,PS5)不符合条件?

这是我使用其中一个reason_Text时的结果,我只选择了重要的列。 enter image description here

我将不胜感激。

5 个答案:

答案 0 :(得分:4)

您需要将PS*REASON TEXT放在使用OR条件的地方,如下所示:

where (
      REASON_TEXT = 'Parts Not available (ASC)'
or    REASON_TEXT = 'Parts In Transit (Samsung)'
or    REASON_TEXT = 'Parts Back Ordered (Samsung)'
      )
and   PS1 = 'U' 
and   ( PS2 = 'U' or PS2 = '' )
and   ( PS3 = 'U' or PS3 = '' )
and   ( PS4 = 'U' or PS4 = '' )
and   ( PS5 = 'U' or PS5 = '' )

请记住,AND运算符优先于OR,并且在组合这些条件时,使用括号非常重要,以便数据库知道评估每个条件的顺序。

完整查询

SELECT TOP 10000 [Service_order]
      ,[COMPANY]
      ,[PENDING_DAYS]
      ,[SERVICE_TYPE]
      ,[SERVICE_TYPE_TXT]
      ,[STATUS]
      ,[STATUS_TEXT]
      ,[REASON]
      ,[REASON_TEXT]
      ,[ASC code]
      ,[ASC name]
      ,[MODEL]
      ,[INOUTWTY]
      ,[Part_code1]
      ,[PS1]
      ,[confirmation_No1]
      ,[Part_code2]
      ,[PS2]
      ,[SO_NO2]
      ,[Part_code3]
      ,[PS3]
      ,[SO_NO3]
      ,[Part_code4]
      ,[PS4]
      ,[SO_NO4]
      ,[Part_code5]
      ,[PS5]
      ,[SO_NO5]
      ,[Engineer name]
  FROM ['NewLP']
  where (
        REASON_TEXT = 'Parts Not available (ASC)'
  or    REASON_TEXT = 'Parts In Transit (Samsung)'
  or    REASON_TEXT = 'Parts Back Ordered (Samsung)'
        )
  and   PS1 = 'U' 
  and   ( PS2 = 'U' or PS2 = '' )
  and   ( PS3 = 'U' or PS3 = '' )
  and   ( PS4 = 'U' or PS4 = '' )
  and   ( PS5 = 'U' or PS5 = '' )

答案 1 :(得分:2)

where子句中的执行顺序不正确。控制如下所示

where (REASON_TEXT = 'Parts Not available (ASC)'
or  REASON_TEXT = 'Parts In Transit (Samsung)'
or REASON_TEXT = 'Parts Back Ordered (Samsung)')
  and PS1 = 'U' 
  and (PS2 = 'U' or PS2 = '')
  and (PS3 = 'U' or PS3 = '')
  and (PS4 = 'U' or PS4 = '')
  and (PS5 = 'U' or PS5 = '')

答案 2 :(得分:1)

尝试添加这样的括号:

  ...
  and PS1 = 'U' 
  and (PS2 = 'U' or PS2 = '')
  and (PS3 = 'U' or PS3 = '')
  and (PS4 = 'U' or PS4 = '')
  and (PS5 = 'U' or PS5 = '')

答案 3 :(得分:1)

你必须使用括号。例如,AND is evaluated before OR in case of no brackets. PS1 = 'U' and PS2 = 'U' or PS2 = '' and PS3 = 'U'(PS1 = 'U' and PS2 = 'U') or (PS2 = '' and PS3 = 'U')具有相同的含义。

答案 4 :(得分:1)

问题是AND的优先级高于OR运算符。所以你需要正确的括号来控制执行顺序。

您的Where子句可以像这样简化

where REASON_TEXT in ('Parts Not available (ASC)',
                      'Parts In Transit (Samsung)',
                      'Parts Back Ordered (Samsung)')
  and PS1 = 'U' 
  and PS2 in ('U','')
  and PS3 in ('U','')
  and PS4 in ('U','')
  and PS5 in ('U','')