SQL条款没有返回正确的结果

时间:2016-10-11 15:07:31

标签: sql where

SELECT * FROM [dbo].[_5200_Sanoma] 
WHERE right(left([VARIABLE1)],4),2) = 'RI' 
and (
        ([Year] = '2014' and [Period] in('10','11','12'))
        or [Year] = '2015') 
        or  (
                [Year] = '2016' 
                and [Period] in('01','02','03','04','05','06','07','08')--,'09','10','11','12')
            )
and ([VARIABLE2] IN(
'String1',
'String2',
'String3',
'String4',
'String5',
'String6',
))

我不得不改变一些事情以使其更加通用但由于某种原因第一个where子句,right(left([VARIABLE1)],4),2) = 'RI'无效,因为我得到的结果是其他两个字符串。

在我添加最后一个where子句条件

之前,此查询已经有效
   and ([VARIABLE2] IN(
    'String1',
    'String2',
    'String3',
    'String4',
    'String5',
    'String6',
    ))

所以现在我补充说它不起作用。有任何想法吗?

2 个答案:

答案 0 :(得分:0)

猜猜你的条款有一些括号问题({ "abc":123, "bcd":"qwe", "cde":"asd", "def":1, "obj.av":861, "obj.asdf.id":139204, "obj.asdf.params.item":139070, "obj.asdf.params.level":110 } 之后的AND部分中没有其中一个

尝试

WHERE right(left([VARIABLE1)],4),2) = 'RI'

答案 1 :(得分:0)

尝试将您的陈述分成不同的行;你可能打算这样做:

SELECT * FROM [dbo].[_5200_Sanoma] 
WHERE right(left([VARIABLE1)],4),2) = 'RI' AND
(
    ([Year] = '2014' and [Period] in('10','11','12')) OR
    ([Year] = '2015') OR
    ([Year] = '2016' and [Period] in('01','02','03','04','05','06','07','08'))
)
and ([VARIABLE2] IN ('String1','String2','String3','String4','String5','String6'))