SQL select语句返回错误数据(where子句使用<>)

时间:2013-12-06 17:14:12

标签: sql select where

有人可以帮助我使用以下代码。即使使用<>也会返回结果我以为会把事情排除在外。我根本不熟悉sql并且会感谢任何和所有的帮助。提前谢谢。

    SELECT k109.keyvaluechar as [Transaction Number], k3257.keyvaluechar as [Fund Name], k4666.keyvaluechar as [Fund ID], k4667.keyvaluechar as [Investor Legal Name], k4638.keyvaluechar as [Transaction Type], k4740.keyvaluecurr as [Cash Transaction Amount], k4701.keyvaluedate as [Effective Date], lc.lifecyclename as [Life Cycle], lcs.statename as [Queue], k4795.keyvaluechar as [Missing Document]
FROM hsi.itemdata id
    LEFT JOIN hsi.keyitem109 k109 on k109.itemnum = id.itemnum
    LEFT JOIN hsi.keyitem3257 k3257 on k3257.itemnum = id.itemnum
    LEFT JOIN hsi.keyitem4666 k4666 on k4666.itemnum = id.itemnum
    LEFT JOIN hsi.keyitem4667 k4667 on k4667.itemnum = id.itemnum
    LEFT JOIN hsi.keyitem4638 k4638 on k4638.itemnum = id.itemnum
    LEFT JOIN hsi.keyitem4740 k4740 on k4740.itemnum = id.itemnum
    LEFT JOIN hsi.keyitem4701 k4701 on k4701.itemnum = id.itemnum
    LEFT JOIN hsi.keyitem4795 k4795 on k4795.itemnum = id.itemnum
    LEFT JOIN hsi.itemlc ilc on ilc.itemnum = id.itemnum
    LEFT JOIN hsi.lcstate lcs on lcs.statenum = ilc.statenum
    JOIN hsi.lifecycle lc on lc.lcnum = lcs.scope
          and (lc.lifecyclename = 'Client Transactions'
          or lc.lifecyclename = 'NEW IS Additional Subscription'
          or lc.lifecyclename = 'NEW IS New Subscription'
          or lc.lifecyclename = 'NEW IS Redemption'
          or lc.lifecyclename = 'NEW IS Transfer')
WHERE 
    id.status = 0 AND
    id.institution = 13 AND
        (lcs.statename <> 'Client Transactions - Distribution' OR
        lcs.statename <> 'Client Transactions - Initiate' OR
        lcs.statename <> 'Client Transactions - Notifications' OR
        lcs.statename <> 'Client Transactions - FTP Process' OR
        lcs.statename <> 'Client Transactions - Import FTP Docs') 
order by lc.lifecyclename, 
    CASE 
    WHEN lcs.statename = 'Client Transactions - Pending/Issue' THEN 1
    WHEN lcs.statename = 'Client Transactions - SEI Review' THEN 2
    WHEN lcs.statename = 'Client Transactions - Missing Documents' THEN 3
    WHEN lcs.statename = 'Client Transactions - Complete' THEN 4
    WHEN lcs.statename = 'Client Transactions - 1st Client Review' THEN 5
    WHEN lcs.statename = 'Client Transactions - 2nd Client Review' THEN 6
    WHEN lcs.statename = 'Client Transactions - Rejected' THEN 7
    WHEN lcs.statename = 'Additional Subscription - Review Document' THEN 8
    WHEN lcs.statename = 'Additional Subscription - Archive' THEN 9
    WHEN lcs.statename = 'New Subscription - Review Document' THEN 10
    WHEN lcs.statename = 'New Subscription - Archive' THEN 11
    WHEN lcs.statename = 'New Subscription - FATCA Follow-Up' THEN 12
    WHEN lcs.statename = 'Redemption - Review Document' THEN 13
    WHEN lcs.statename = 'Redemption - Holdback' THEN 14
    WHEN lcs.statename = 'Redemption - Archive' THEN 15
    WHEN lcs.statename = 'Redemption - FATCA Follow-Up' THEN 16
    WHEN lcs.statename = 'Transfer - Review Document' THEN 17
    WHEN lcs.statename = 'Transfer - Archive' THEN 18
    WHEN lcs.statename = 'Transfer - FATCA Follow-Up' THEN 19
    ELSE 21
end,
    k109.keyvaluechar asc, k3257.keyvaluechar asc

2 个答案:

答案 0 :(得分:1)

(lcs.statename <> 'Client Transactions - Distribution' OR
        lcs.statename <> 'Client Transactions - Initiate' OR
        lcs.statename <> 'Client Transactions - Notifications' OR
        lcs.statename <> 'Client Transactions - FTP Process' OR
        lcs.statename <> 'Client Transactions - Import FTP Docs') 

在上面部分用AND

替换所有OR

答案 1 :(得分:1)

如果你在谈论这个代码块

id.institution = 13 AND
        (lcs.statename <> 'Client Transactions - Distribution' OR
        lcs.statename <> 'Client Transactions - Initiate' OR
        lcs.statename <> 'Client Transactions - Notifications' OR
        lcs.statename <> 'Client Transactions - FTP Process' OR
        lcs.statename <> 'Client Transactions - Import FTP Docs')

这是因为你在一个或陈述中使用的不是等号。这意味着无论lcs.statename是什么,括号段返回的布尔值将始终为true,因为即使语句是“客户端事务 - 分发”也是如此。它不是&#39;客户交易 - 启动&#39;这样或链条将得到满足并返回真实。基本上你的陈述看起来像id.institution = 13 AND true

如果它没有列出任何字符串,你希望它返回false,或者如果它不是其中之一,你希望它返回true

无论哪种方式,您都应该切换所有&lt;&gt;到=。这将检查语句是否与这些字符串中的任何一个匹配。如果确实找到匹配,则返回true。如果你想让它返回false,那么在括号前添加一个不反转它。

id.institution = 13 AND NOT
        (lcs.statename <> 'Client Transactions - Distribution' AND
        lcs.statename <> 'Client Transactions - Initiate' AND
        lcs.statename <> 'Client Transactions - Notifications' AND
        lcs.statename <> 'Client Transactions - FTP Process' AND
        lcs.statename <> 'Client Transactions - Import FTP Docs')