2011-11-08 87 views
-2

我有兩個表。客戶(客戶,公司,州,地位)和支持狀態(客戶,公司,地位,協議)。下面的查詢是否應該返回協議值爲0且狀態不等於「禁用」的所有公司? 我很困惑,因爲我正在選擇公司,我正在加入,這兩個表由公共因素(clientid)組成,我正在篩選查詢的結果。SQL連接問題澄清

SELECT cl.company 
FROM clients cl 
     INNER JOIN supportstatus su 
     ON cl.clientid = su.clientid 
WHERE su.agreement11 = 0 
     AND su.status <> 'disabled' 
ORDER BY cl.company 

的ColdFusion源

<cfquery name="qryPendingAgreement" datasource="support"> 
SELECT clientid 
FROM supportstatus 
WHERE agreement11 = 0 AND status <> 'disabled' 
</cfquery> 

<cfquery name="qryClient" datasource="support"> 
SELECT  clientid, company, state, serv_billing 
FROM   clients 
WHERE prod_arth = 1 OR prod_artr = 1 OR prod_epcr_host = 1 OR prod_epcr_remote = 1 OR prod_billing = 1 OR prod_collections = 1 
</cfquery> 

<cfquery name="qryResults" dbtype="query"> 
SELECT qryClient.company, qryClient.state, qryClient.serv_billing 
FROM qryPendingAgreement, qryClient 
WHERE qryPendingAgreement.clientid = qryClient.clientid 
order by qryClient.company 
</cfquery> 
+2

不知道你的問題是什麼?如果它沒有返回您期望的結果,請提供示例源數據和期望的結果。 –

+0

究竟發生了什麼意外的結果?查詢格式正確。 –

+0

沿着什麼@MartinSmith說,可以su.status包含NULL值? – tawman

回答

0

這是胡亂猜測,但如果你正在使用這個版本:

SELECT cl.company 
FROM clients cl 
     INNER JOIN supportstatus su 
     ON cl.clientid = su.clientid 
WHERE su.agreement11 = 0 
     AND su.status <> 'disabled' 
     OR prod_arth = 1  OR prod_artr = 1 
     OR prod_epcr_host = 1 OR prod_epcr_remote = 1 
     OR prod_billing = 1 OR prod_collections = 1 
ORDER BY cl.company 

的問題是AND,OR優先。試試這個:

SELECT cl.company 
FROM clients cl 
     INNER JOIN supportstatus su 
     ON cl.clientid = su.clientid 
WHERE su.agreement11 = 0 
     AND su.status <> 'disabled' 
     AND (prod_arth = 1  OR prod_artr = 1 
      OR prod_epcr_host = 1 OR prod_epcr_remote = 1 
      OR prod_billing = 1 OR prod_collections = 1 
      ) 
ORDER BY cl.company 
1

你沒說什麼結果的查詢是給你的。如果查詢過濾的行數多於您認爲的數量,則答案可能是三值邏輯。

null != 'disabled' - >null,這是不正確的,該行被過濾。

請嘗試使用此過濾器。

WHERE su.agreement11 = 0 
    AND isnull(su.status,'') <> 'disabled' 
+0

對不起。是的,我的查詢返回5個以上的結果,比CF版本 – MasterP

+0

@MasterP,CF查詢有更多的過濾器:'prod_arth = 1 OR ...' –

+0

SELECT cl.company FROM clients cl INNER JOIN supportstatus su ON cl.clientid = su .clientid WHERE su.agreement11 = 0 AND isnull(su.status,'')<>'disabled'Order By cl.company =調用本地函數'isnull'時參數數不正確 – MasterP

0

這應該有助於您測試您的查詢。你的問題不夠具體,所以,這是最好的,我可以做的:)

DECLARE @client TABLE 
    (
     clientid int, 
     company VARCHAR(50) 
    ) 
    DECLARE @supportstatus TABLE 
    (
     clientid int, 
     [status] VARCHAR(50), 
     agreement INT 
    ) 

    INSERT INTO @client ([clientid],[company]) VALUES (0,'acme') 
    INSERT INTO @client ([clientid],[company]) VALUES (1,'byron') 
    INSERT INTO @client ([clientid],[company]) VALUES (2,'cathode') 

    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (0, 'disabled',0) 
    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (0, 'disabled',1) 
    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (0, 'somethingelse',0) 
    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (0, 'somethingelse',1) 

    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (1, 'disabled',0) 
    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (1, 'disabled',1) 
    --INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (1, 'somethingelse',0) 
    --INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (1, 'somethingelse',1) 

    --INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (2, 'disabled',0) 
    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (2, 'disabled',1) 
    --INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (2, 'somethingelse',0) 
    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (2, 'somethingelse',1) 

    -- all companies who have su.agreement = 0 and su.status <> 'disabled' 
    SELECT cl.company 
    FROM @client cl 
    JOIN @supportstatus su ON cl.clientid = su.clientid 
    WHERE su.agreement = 0 AND su.status <> 'disabled' 
    Order By cl.company 
+0

感謝您的幫助 – MasterP