奇怪的结果

时间:2016-08-09 22:13:58

标签: sql-server-2005 where-clause

我正在使用此查询从尚未付款的客户处获得付款。所以我需要在where子句中排除所有类型。我尝试使用<>和!=并且不喜欢,但它向我显示所有付款,也包括我在where子句中排除的那些付款。你看到我看不到的东西吗?

SELECT td= CODCLIFOR,      '', 
            td= DSCCONTO1,      '',  
            td= ANAGRAFICACF.TELEX,      '', 
            td= NUMDOC,      '', 
            td= NUMSCAD,      '', 
            td= CONVERT (VARCHAR(30),DATASCADENZA,105),      '', 
            td= '€ ' + REPLACE(CONVERT(varchar, CAST(IMPORTOSCLIT AS money), 105),',','.') ,       '', 
            td= TIPIEFFETTI.DESCRIZIONE,ESITO
            FROM dbo.TABSCADENZE
            INNER JOIN dbo.ANAGRAFICACF ON CODCLIFOR = CODCONTO
            INNER JOIN dbo.TIPIEFFETTI ON TIPOEFFETTO = EFFETTO
            INNER JOIN dbo.BANCAAPPCF ON CODCLIFOR = BANCAAPPCF.CODCONTO AND BANCAAPPCF.CODICE = TABSCADENZE.BANCAAPPOGGIO
            INNER JOIN  dbo.ANAGRAFICABANCHE ON BANCAINC = CODBANCA
            WHERE  (TABSCADENZE.TIPOEFFETTO NOT LIKE '%2%' OR TABSCADENZE.TIPOEFFETTO NOT LIKE '%10%') AND (TABSCADENZE.ESITO NOT LIKE '%1%' OR  TABSCADENZE.ESITO NOT LIKE '%2%' OR  TABSCADENZE.ESITO NOT LIKE '%4%')  AND  CODCLIFOR LIKE '%C%'
            ORDER BY CODCLIFOR DESC

谢谢!

2 个答案:

答案 0 :(得分:0)

这是ORsANDs的组合,并使用令人困惑和错误的NOT

不要像英语那样阅读它们,因为这是大多数人错误地认为他们编写了正确逻辑的地方。

如果对NOT使用OR,则需要执行相反操作并将OR替换为OR。否则一个会失败,但另一个会失败,因此整个WHERE变为TRUE,这就是你所有记录都显示的原因。

试试这个......

WHERE
    TABSCADENZE.TIPOEFFETTO NOT LIKE '%2%' 
AND TABSCADENZE.TIPOEFFETTO NOT LIKE '%10%'
AND TABSCADENZE.ESITO NOT LIKE '%1%' 
AND TABSCADENZE.ESITO NOT LIKE '%2%' 
AND TABSCADENZE.ESITO NOT LIKE '%4%'
AND CODCLIFOR LIKE '%C%'

看看这个类似的......

How do I add multiple "NOT LIKE '%?%' in the WHERE clause of sqlite3 in python code?

答案 1 :(得分:0)

好的,我找到了解决方案。我使用了Not In with子查询。现在结果很好。这是代码,如果有人需要它:

WHERE DATASCADENZA BETWEEN DATEADD(DAY, -30, GETDATE()) AND DATEADD(DAY, -15, GETDATE()) AND CODCLIFOR LIKE '%C%' 
                AND TIPOEFFETTO NOT IN (SELECT DISTINCT TIPOEFFETTO FROM dbo.TABSCADENZE WHERE TIPOEFFETTO = 10 OR TIPOEFFETTO = 2 ) AND ESITO NOT IN (SELECT DISTINCT ESITO FROM dbo.TABSCADENZE WHERE ESITO = 1 OR ESITO = 2 OR ESITO = 4)

感谢您的帮助!