2014-10-28 57 views
0

因此,我一直在替換我的應用程序中的所有舊查詢,以刪除*=運算符。在其中一個查詢中,我在查找爲什麼輸出不同時遇到了一些問題。更新後的SQL查詢返回無數據,即使有交叉連接

這是舊的查詢:

SELECT 
    URMS_User_Name AS FE_Name, 
    URMS_User_ID AS FE_Code, 
    COUNT(MRFS_FE_Code) AS No_Of_Jobs, 
    SCRM_Service_Center_Name AS Service_Center_Name, 
    CSMSDVLP.fnGetExpertise(URMS_User_ID) AS Expertise 
FROM 
    TBL_User_Master, 
    TBL_Service_Center_Master, 
    TBL_User_Role, 
    TBL_MR_FE_Schedule 
WHERE     
    USRL_Role_Code = 'FE' AND 
    URMS_User_ID = USRL_User_ID AND  
    SCRM_Service_Center_ID = URMS_Service_Center_ID AND 
    URMS_User_ID *= MRFS_FE_Code AND 
    MRFS_Scheduled_Date >= '28-Oct-2014 00:00:00' AND 
    MRFS_Scheduled_Date <= '04-Nov-2014 00:00:00' 
GROUP BY 
    SCRM_Service_Center_Name, 
    URMS_User_Name, 
    URMS_User_ID 
ORDER BY 
    URMS_User_ID 

這是更新查詢:

SELECT 
    URMS_User_Name AS FE_Name, 
    URMS_User_ID AS FE_Code, 
    COUNT(MRFS_FE_Code) AS No_Of_Jobs, 
    SCRM_Service_Center_Name AS Service_Center_Name, 
    CSMSDVLP.fnGetExpertise(URMS_User_ID) AS Expertise 
FROM 
    TBL_User_Master 
    Left Join TBL_MR_FE_Schedule 
     ON URMS_User_ID = MRFS_FE_Code 
    Inner Join TBL_Service_Center_Master 
     ON SCRM_Service_Center_ID = URMS_Service_Center_ID 
    Inner Join TBL_User_Role 
     ON URMS_User_ID = USRL_User_ID 
WHERE 
    USRL_Role_Code = 'FE' 
    AND MRFS_Scheduled_Date >= '28-Oct-2014 00:00:00' 
    AND MRFS_Scheduled_Date <= '04-Nov-2014 00:00:00' 
GROUP BY 
    SCRM_Service_Center_Name, 
    URMS_User_Name, 
    URMS_User_ID 
ORDER BY 
    URMS_User_ID 

舊的查詢返回的數據完全正常,但新的查詢返回的空行。我嘗試用交叉連接和同樣的問題替換所有連接。

雖然奇怪的是如果我運行沒有日期條件的查詢,一切似乎工作正常。所以,現在,我很困惑我是否犯了一個錯誤,或者它只是一個瘋狂的老運營商。

回答

1

外連接表上的條件屬於ON子句。外連接的記錄具有NULL的MRFS_Scheduled_Date,所以在您的WHERE子句中刪除這些,從而將外連接變爲內連接。由於WHERE子句比以前更嚴格,這可能是沒有得到任何結果的原因。

SELECT 
    URMS_User_Name AS FE_Name, 
    URMS_User_ID AS FE_Code, 
    COUNT(MRFS_FE_Code) AS No_Of_Jobs, 
    SCRM_Service_Center_Name AS Service_Center_Name, 
    CSMSDVLP.fnGetExpertise(URMS_User_ID) AS Expertise 
FROM TBL_User_Master 
LEFT JOIN TBL_MR_FE_Schedule ON URMS_User_ID = MRFS_FE_Code AND MRFS_Scheduled_Date >= '28-Oct-2014 00:00:00' AND MRFS_Scheduled_Date <= '04-Nov-2014 00:00:00' 
INNER JOIN TBL_Service_Center_Master ON SCRM_Service_Center_ID = URMS_Service_Center_ID 
INNER JOIN TBL_User_Role ON URMS_User_ID = USRL_User_ID 
WHERE USRL_Role_Code = 'FE' 
GROUP BY SCRM_Service_Center_Name, URMS_User_Name, URMS_User_ID 
ORDER BY URMS_User_ID; 
+0

所以,我應該將日期條件移動到Join子句?好吧,讓我儘快嘗試,並回到你身邊。 它的工作原理。我不知道,我的外部連接自動轉換爲內部連接。 – jitendragarg 2014-10-28 11:10:38