WHERE <cond> OR <cond>但不是两者兼而有之

时间:2016-11-02 14:52:41

标签: sql sql-server toto

我正在尝试找到以下设备:

  • “实例名称”
  • 中不存在“PatchManagementPremium”
  • “instancename
  • ”列中不存在“RemoteControl”
  • 列instancename
  • 中不存在这两个条目

但是我想排除两个条目都为真的所有结果,直到现在我都没有这样做。无论我尝试什么,当1和2都是真的时候,它就像是假的......

SELECT DISTINCT 
    Devices.DeviceName
FROM
    Devices
LEFT OUTER JOIN 
    CustInv_ObjType_6121 BCMModulesVersions ON Devices.DeviceID = BCMModulesVersions.DeviceID
LEFT OUTER JOIN 
    InventoryIntegrationData InventoryUpdate ON Devices.DeviceID = InventoryUpdate.DeviceID
WHERE  
    ((BCMModulesVersions.InstanceName NOT IN ('PatchManagementPremium', 'RemoteControl'))
    AND (InventoryUpdate.IntegrationDate IS NOT NULL)
    AND Devices.TopologyType IN ('_DB_DEVTYPE_CLIENT_', '_DB_DEVTYPE_RELAY_'))
ORDER BY 
    Devices.DeviceName ASC; 

更清楚:我支持可以加载或不加载多个模块的应用程序。该信息存储在数据库中。加载模块后,您将在该设备的列instancename中找到其名称(remotecontrol,patchmanagementpremium等)。

我想列出未加载模块remotecontrol的所有设备,或者未加载模块补丁或未加载两个模块。

如果两个条目都已加载,则devicename不应位于查询的输出中。

5 个答案:

答案 0 :(得分:1)

如果我理解正确,你基本上可以这样做:

WHERE NOT(1 AND 2) AND (1 OR 2)

答案 1 :(得分:0)

将过滤器移至HAVING子句并执行条件计数

这是一种方法

SELECT Devices.DeviceName
FROM   Devices
       JOIN CustInv_ObjType_6121 BCMModulesVersions
         ON Devices.DeviceID = BCMModulesVersions.DeviceID
       JOIN InventoryIntegrationData InventoryUpdate
         ON Devices.DeviceID = InventoryUpdate.DeviceID
WHERE  ( InventoryUpdate.IntegrationDate IS NOT NULL )
       AND Devices.TopologyType IN ( '_DB_DEVTYPE_CLIENT_', '_DB_DEVTYPE_RELAY_' )
GROUP  BY Devices.DeviceName
HAVING Count(CASE
               WHEN BCMModulesVersions.InstanceName IN ( 'PatchManagementPremium', 'RemoteControl' ) THEN 1
             END) = 0 

答案 2 :(得分:0)

我想你可以在这里使用NOT EXISTS。

SELECT DISTINCT
        Devices.DeviceName
FROM    Devices
        INNER JOIN InventoryIntegrationData InventoryUpdate ON Devices.DeviceID = InventoryUpdate.DeviceID
WHERE   InventoryUpdate.IntegrationDate IS NOT NULL
        AND Devices.TopologyType IN ('_DB_DEVTYPE_CLIENT_','_DB_DEVTYPE_RELAY_')
        AND NOT EXISTS ( SELECT 1
                         FROM   CustInv_ObjType_6121 BCMModulesVersions
                         WHERE  Devices.DeviceID = BCMModulesVersions.DeviceID
                                AND BCMModulesVersions.InstanceName IN ('PatchManagementPremium','RemoteControl'))
ORDER BY Devices.DeviceName ASC; 

如果您只想在两个值都存在的情况下排除,则可以使用。

SELECT DISTINCT
        Devices.DeviceName
FROM    Devices
        JOIN InventoryIntegrationData InventoryUpdate ON Devices.DeviceID = InventoryUpdate.DeviceID
WHERE   InventoryUpdate.IntegrationDate IS NOT NULL
        AND Devices.TopologyType IN ('_DB_DEVTYPE_CLIENT_','_DB_DEVTYPE_RELAY_')
        AND 2 > ( SELECT COUNT(DISTINCT BCMModulesVersions.InstanceName)
                         FROM   CustInv_ObjType_6121 BCMModulesVersions
                         WHERE  Devices.DeviceID = BCMModulesVersions.DeviceID
                                AND BCMModulesVersions.InstanceName IN ('PatchManagementPremium','RemoteControl'))
ORDER BY Devices.DeviceName ASC; 

答案 3 :(得分:0)

我认为你说这两个案件中的一个或两个都缺失了(所以他们都不在场)

SELECT DISTINCT 
    Devices.DeviceName
FROM
    Devices
LEFT OUTER JOIN 
    CustInv_ObjType_6121 BCMModulesVersions ON Devices.DeviceID = BCMModulesVersions.DeviceID
LEFT OUTER JOIN 
    InventoryIntegrationData InventoryUpdate ON Devices.DeviceID = InventoryUpdate.DeviceID
WHERE  

    (NOT EXISTS (SELECT 0 FROM CustInv_ObjType_6121 X WHERE X.InstanceName IN('RemoteControl'))
        OR
     NOT EXISTS (SELECT 0 FROM CustInv_ObjType_6121 Y WHERE Y.InstanceName IN('PatchManagementPremium'))
    )
    AND (InventoryUpdate.IntegrationDate IS NOT NULL)
    AND Devices.TopologyType IN ('_DB_DEVTYPE_CLIENT_', '_DB_DEVTYPE_RELAY_')
ORDER BY 
    Devices.DeviceName ASC; 

答案 4 :(得分:0)

感谢JamieD77,我找到了解决方案! :)

SELECT DISTINCT
        Devices.DeviceName
FROM    Devices
        JOIN InventoryIntegrationData InventoryUpdate ON Devices.DeviceID = InventoryUpdate.DeviceID
        JOIN CustInv_ObjType_6121 BCMModulesVersions ON Devices.DeviceID = BCMModulesVersions.DeviceID
WHERE   InventoryUpdate.IntegrationDate IS NOT NULL
        AND Devices.TopologyType IN ('_DB_DEVTYPE_CLIENT_','_DB_DEVTYPE_RELAY_')
        AND BCMModulesVersions.InstanceName NOT IN ('PatchManagement','RemoteControl')
        AND 2 > ( SELECT COUNT(DISTINCT BCMModulesVersions.InstanceName)
                         FROM   CustInv_ObjType_6121 BCMModulesVersions
                         WHERE  Devices.DeviceID = BCMModulesVersions.DeviceID
                                AND BCMModulesVersions.InstanceName IN ('PatchManagementPremium','RemoteControl'))
ORDER BY Devices.DeviceName ASC; 

感谢大家的帮助!