带有相似和不相似运算符的where子句不起作用

时间:2018-11-12 15:12:31

标签: sql sql-server-2012

我有这个查询:

SELECT 
DISTINCT 
REPLACE(REPLACE(A.[NOME COMERCIAL],'.',''),'&','') AS [NOME COMERCIAL]        ,
ISNULL(A.[PLANO], '') AS PLANO       ,
A.[Plano Foco]        ,
CONVERT(DECIMAL(18,2),A.[PRECO]) AS PRECO        ,
B.[FEAT_CAMERA]        ,
B.[FEAT_TELA]        ,
B.[FEAT_CAP_ARMAZENAMENTO]        ,
B.[FEAT_PROCESSADOR]        ,
B.[FEAT_MEMORIA_RAM]        ,
B.[FEAT_BATERIA]        ,
B.[EXTRA_INFO_1]        ,
B.[EXTRA_INFO_2]        ,
B.[EXTRA_INFO_3]  
FROM [TABELA DE PRECOS] AS A
LEFT JOIN DE_PARA_SAP_APARELHOS AS B
  ON (A.[NOME DPGC] = B.APARELHODPAV)
WHERE PLANO LIKE '%CONTROLE%'
  AND PLANO NOT LIKE '%Renova%'
  AND A.[PRODUTO] IN ('Smartphone', 'Blackbox 3G', 'Blackbox 4G', 'Fixo', 'Modem 3G', 'Modem 4G', 'Tablet')
  AND REPLACE(REPLACE(REPLACE(A.[NOME DPGC],'&',''),'''',''),'.','') = 'Iphone XR 64GB' 
   OR REPLACE(REPLACE(REPLACE(B.[APARELHODPAV],'&',''),'''',''),'.','') = 'Iphone XR 64GB'
  AND A.[REGIAO] = '7x'
ORDER BY PRECO,[PLANO]  

我搜索了一下,发现如果您具有null值,则LIKE和NOT LIKE无法正常工作,因此我正在使用ISNULL。但我仍在获取包含“ Renova”的值:

Novo Vivo V (Renova)
Novo Vivo V
Vivo Família 70GB (Renova)
Vivo Família 50GB (Renova)
Vivo Família 70GB
Vivo Controle....

我没有收到任何错误消息。该查询运行正常,但是就像我的“喜欢”和“不喜欢”运算符被忽略之类。我在这里想念什么?

2 个答案:

答案 0 :(得分:0)

OR子句中缺少括号:

SELECT DISTINCT REPLACE(REPLACE(A.[NOME COMERCIAL],'.',''),'&','') AS [NOME COMERCIAL]        ,
                ISNULL(A.[PLANO], '') AS PLANO       ,
                A.[Plano Foco]        ,
                CONVERT(DECIMAL(18,2),A.[PRECO]) AS PRECO        ,
                B.[FEAT_CAMERA]        ,
                B.[FEAT_TELA]        ,
                B.[FEAT_CAP_ARMAZENAMENTO]        ,
                B.[FEAT_PROCESSADOR]        ,
                B.[FEAT_MEMORIA_RAM]        ,
                B.[FEAT_BATERIA]        ,
                B.[EXTRA_INFO_1]        ,
                B.[EXTRA_INFO_2]        ,
                B.[EXTRA_INFO_3]  
FROM @A AS A 
    LEFT JOIN @B AS B  ON 
    A.[NOME DPGC] = B.APARELHODPAV
WHERE   PLANO LIKE '%CONTROLE%'  AND 
        PLANO NOT LIKE '%Renova%'  AND 
        A.[PRODUTO] IN ('Smartphone', 'Blackbox 3G', 'Blackbox 4G', 'Fixo', 'Modem 3G', 'Modem 4G', 'Tablet')  AND 
        (REPLACE(REPLACE(REPLACE(A.[NOME DPGC],'&',''),'''',''),'.','') = 'Iphone XR 64GB' OR REPLACE(REPLACE(REPLACE(B.[APARELHODPAV],'&',''),'''',''),'.','') = 'Iphone XR 64GB') AND 
        A.[REGIAO] = '7x' 
ORDER BY PRECO,[PLANO]  

答案 1 :(得分:-1)

如果您不想包括Renova,请不要在您的where子句中包括它,它将不受选择。这样可以解决您的问题。