2014-09-26 96 views
0

我有一個使用*=運算符的舊查詢。眼下,查詢有哪裏像下面瞭解3個或更多表格的連接順序

Table1.Column1 *= Table2.Column1 
AND Table1.Column2 *= Table3.Column1 
if (some conditions in C# script) //this whole clause is generated by C# function based on different conditions 
AND Table1.Column3 *= Table4.Column1 

我不得不重寫它使用左連接,因爲好,我們是不是恐龍了,而且正在向SQL服務器2014(從SQL Server 2000)條款。反正,我已經改寫像

From Table1 
Left Join Table2 On Table1.Column1 = Table2.Column1 
Left Join Table3 On Table1.Column2 = Table3.Column1 
Left Join Table4 On Table1.Column3 = Table4.Column1 

查詢我相信這應該提供我同樣的結果集,但事實並非如此。顯然SQL Server在這兩種情況下都不遵循相同的連接順序。所以,我必須找出舊查詢所遵循的確切順序,以及如何重新創建相同的順序。

P.S.我對代碼沒有太多的瞭解。但是,我可以在這裏發佈完整的功能,以幫助某人更好地理解情況。

編輯:

確切的查詢設計器功能,我使用。

public virtual FANUC.Common.BaseClasses.Row[] GetCustomersForPopup(FANUC.Common.BaseClasses.Row objListCustomerFilter, FANUC.Common.BaseClasses.PagingEventArgs e) { 

     string strConnector = " WHERE "; 
     string strANDClause = ""; 

     string strSQLQuery = " SELECT " 
          + " TBL_Company_Master.CMPM_Company_ID," 
          + " TBL_Company_Master.CMPM_Company_Name," 
          + " " + ((FANUCUser)Thread.CurrentPrincipal.Identity).DBUser + ".fnGetRefCodeValue(CMPM_Company_Type_ID) AS CMPM_CompanyTypeID," 
          + " TBL_Company_Master.CMPM_Company_NickName," 
          + " TBL_Company_Master.CMPM_Service_Center_ID," 
          + " TBL_Company_Master.CMPM_Company_BranchName," 
          + " TBL_Company_Master.CMPM_Black_Listed_Flag," 
          + " TBL_Company_Master.CMPM_Prohibited_Company_Flag," 
          + " " + ((FANUCUser)Thread.CurrentPrincipal.Identity).DBUser + ".fnGetRefCodeValue(TBL_Company_Master.CMPM_Status) AS CMPM_Status," 
          + " TBL_Company_Master.CMPM_City_Location_ID AS CMPM_City_Location_ID," 
          + " TBL_City_Location_Master.CLIM_City_Name AS CLIM_City_Name, " 
          + " TBL_Company_Master.CMPM_Country_ID AS CMPM_Country_ID," 
          + " TBL_Country_Master.CRIM_CountryName, " 
          + " TBL_Company_Master.CMPM_Night_Call_Applicable_flag," 
          + " TBL_Company_Master.CMPM_Default_currency_for_transaction," 
          + " TBL_Company_Master.CMPM_Telephone_No, " 
          + " TBL_Customer_Contact_Master.CNTM_ContactPersonName, " 
          + " TBL_Customer_Contact_Master.CNTM_Section_Name, " 
          + " TBL_Company_Master.Use_Count, " 
          + " TBL_Company_Master.CMPM_Self_Company_Indicator, " 
          + " TBL_Company_Master.CMPM_Transport_Time "; 


     string strFromClause = " FROM TBL_Company_Master, " 
           + " TBL_Service_Center_Master, " 
           + " TBL_City_Location_Master, " 
           + " TBL_Country_Master, " 
           + " TBL_Customer_Contact_Master"; 

     strANDClause += " AND TBL_Company_Master.CMPM_Service_Center_ID *= TBL_Service_Center_Master.SCRM_Service_Center_ID " 
         + " AND TBL_Company_Master.CMPM_City_Location_ID *= TBL_City_Location_Master.CLIM_City_ID " 
         + " AND TBL_Company_Master.CMPM_Country_ID *= TBL_Country_Master.CRIM_CountryID "; 

      if (objListCustomerFilter[ Constants.IS_CALLING_CUSTOMER ] != null || objListCustomerFilter[ Constants.IS_PAYEE_CUSTOMER ] != null || Convert.ToInt32(objListCustomerFilter[ "CUTM_Customer_Type_ID" ]) == 120) 
       strANDClause += " AND TBL_Company_Master.CMPM_Company_ID *= TBL_Customer_Contact_Master.CNTM_Customer_ID "; 
      else 
       strANDClause += " AND TBL_Company_Master.CMPM_Company_ID = TBL_Customer_Contact_Master.CNTM_Customer_ID " ; 

      strANDClause += " AND TBL_Customer_Contact_Master.CNTM_Default_Flag = 'Y' "; 
      strANDClause += " AND CMPM_Active_Flag != 'N'"; 


     if (objListCustomerFilter["CUTM_Customer_Type_ID"] != null && Convert.ToString(objListCustomerFilter["CUTM_Customer_Type_ID"]) != "") { 
      strFromClause += " ,TBL_Customer_Type_Mapping "; 
      strANDClause += " AND CUTM_Customer_ID = CMPM_Company_ID " + " AND CUTM_Customer_Type_ID = "+Convert.ToString(objListCustomerFilter["CUTM_Customer_Type_ID"]); 
     } 

     if (objListCustomerFilter["CMPM_Company_Type_ID"] != null && Convert.ToString(objListCustomerFilter["CMPM_Company_Type_ID"]) != "" && Convert.ToString(objListCustomerFilter["CMPM_Company_Type_ID"]) != Constants.ALL) { 
      strANDClause += " AND CMPM_Company_Type_ID IN ("+Convert.ToString(objListCustomerFilter["CMPM_Company_Type_ID"])+","+Constants.COMPANY_TYPE_BOTH+") "; 
     } 

     if (!Convert.ToString(objListCustomerFilter[ Constants.PAYMENT_REQD ]).Equals(Constants.CONST_NO)) { 

      strSQLQuery += ", TBL_Company_Payment_Terms.CMPT_Payment_Term_Description " 
         + ", TBL_Company_Payment_Terms.CMPT_Payment_Term_ID "; 

      strFromClause += " ,TBL_Company_Payment_Terms "; 

      if((objListCustomerFilter[Constants.IS_CALLING_CUSTOMER] != null) ||(objListCustomerFilter[Constants.IS_END_USER] != null)) 
       strANDClause += " AND TBL_Company_Master.CMPM_Company_ID *= TBL_Company_Payment_Terms.CMPT_Company_ID " 
           + " AND TBL_Company_Payment_Terms.CMPT_Default = 'Y' "; 
      else 
       strANDClause += " AND TBL_Company_Master.CMPM_Company_ID = TBL_Company_Payment_Terms.CMPT_Company_ID " 
           + " AND TBL_Company_Payment_Terms.CMPT_Default = 'Y' "; 


      if (objListCustomerFilter[ "CMPM_Company_Type_ID" ] != null && Convert.ToString(objListCustomerFilter[ "CMPM_Company_Type_ID" ]) != Constants.COMPANY_TYPE_BOTH && Convert.ToString(objListCustomerFilter[ "CMPM_Company_Type_ID" ]) != Constants.ALL) 
       strANDClause += " AND CMPT_Company_Type_ID = " + Convert.ToString(objListCustomerFilter[ "CMPM_Company_Type_ID" ]); 

     } 

     strANDClause += " AND CMPM_Subsidiary_Code = '"+((FANUCUser)Thread.CurrentPrincipal.Identity).SubsidiaryCode+"'"; 

     Row objFilter = new Row(); 

     objFilter["CMPM_Company_ID"]       = objListCustomerFilter["CMPM_Company_ID"]; 
     objFilter["CMPM_Black_Listed_Flag"]      = objListCustomerFilter["CMPM_Black_Listed_Flag"]; 
     objFilter["CMPM_Prohibited_Company_Flag"]    = objListCustomerFilter["CMPM_Prohibited_Company_Flag"]; 
     objFilter["CMPM_Status"]        = objListCustomerFilter["CMPM_Status"]; 
     objFilter["CMPM_Company_Name~like"]      = objListCustomerFilter["CMPM_Company_Name"]; 
     objFilter["CMPM_Company_NickName~like"]     = objListCustomerFilter["CMPM_Company_NickName"]; 
     objFilter["CMPM_Telephone_No~like"]      = objListCustomerFilter["CMPM_Telephone_No"]; 
     objFilter["CMPM_FAX_No"]        = objListCustomerFilter["CMPM_FAX_No"]; 
     objFilter["CMPM_Service_Center_ID"]      = objListCustomerFilter["CMPM_Service_Center_ID"]; 
     objFilter["CMPM_Billing_Company_ID"]     = objListCustomerFilter["CMPM_Billing_Company_ID"]; 
     objFilter["CMPM_Shipping_Company_ID"]     = objListCustomerFilter["CMPM_Shipping_Company_ID"]; 
     objFilter["CMPM_City_Location_ID"]      = objListCustomerFilter["CMPM_City_Location_ID"]; 
     objFilter["CMPM_State_ID"]        = objListCustomerFilter["CMPM_State_ID"]; 
     objFilter["CMPM_Country_ID"]       = objListCustomerFilter["CMPM_Country_ID"]; 
     objFilter["CMPM_Grp_Parent_Company_ID"]     = objListCustomerFilter["CMPM_Grp_Parent_Company_ID"]; 
     objFilter["CMPM_Night_Call_Applicable_Flag"]   = objListCustomerFilter["CMPM_Night_Call_Applicable_Flag"]; 
     objFilter["CMPM_Default_currency_for_transaction"]  = objListCustomerFilter["CMPM_Default_currency_for_transaction"]; 
     objFilter["CMPM_Company_local_registration_No~like"] = objListCustomerFilter["CMPM_Company_local_registration_No"]; 
     objFilter["CMPM_Company_central_registration_No~like"] = objListCustomerFilter["CMPM_Company_central_registration_No"]; 
     objFilter["CMPM_Insurance_Policy_No~like"]    = objListCustomerFilter["CMPM_Insurance_Policy_No"]; 
     objFilter["CMPM_Active_Flag"]       = objListCustomerFilter["CMPM_Active_Flag"]; 
     objFilter["CMPM_Company_BranchName~like"]    = objListCustomerFilter["CMPM_Company_BranchName"]; 
     objFilter["CMPM_Company_BranchName_LocalLanguage~like"] = objListCustomerFilter["CMPM_Company_BranchName_LocalLanguage"]; 
     objFilter["CMPM_Postal_Code"]       = objListCustomerFilter["CMPM_Postal_Code"]; 
     objFilter["CMPM_Web_Site~like"]       = objListCustomerFilter["CMPM_Web_Site"]; 
     objFilter["CMPM_Distance"]        = objListCustomerFilter["CMPM_Distance"]; 

     if (objListCustomerFilter["CMPM_Self_Company_Indicator"] != null && Convert.ToString(objListCustomerFilter["CMPM_Self_Company_Indicator"]) != Constants.ALL) 
      objFilter[ "CMPM_Self_Company_Indicator" ] = objListCustomerFilter["CMPM_Self_Company_Indicator"]; 



     CommonBQ objCommonBQ  = new CommonBQ(); 
     string  strSearchClause = objCommonBQ.CreateFilter(objFilter); 
     string  strFinalString = ""; 

     if (!strSearchClause.Equals("")) strFinalString = strSQLQuery + strFromClause + strConnector + strSearchClause + strANDClause; 
     else { 

      strSQLQuery += strFromClause + strConnector + strANDClause; 

      int  iFirstPos  = strSQLQuery.IndexOf("AND", 0); 
      string strFirstPart = strSQLQuery.Substring(0, iFirstPos); 
      string strSecondPart = strSQLQuery.Substring(iFirstPos + 3, strSQLQuery.Length - iFirstPos - 3); 

      strFinalString = strFirstPart + strSecondPart; 
     } 

     return GetRows(strFinalString, CreateParameterArray(objListCustomerFilter), CommandType.Text, null, e); 
    } 
+0

我不明白這個問題嗎?第一個和第二個給你什麼輸出? – Arion 2014-09-26 06:22:52

+0

請分享整個'where'子句.. – Deepshikha 2014-09-26 06:22:57

+0

新的查詢返回原始行的大約一半的行。我將在主要評論中添加完整的條款。 – jitendragarg 2014-09-26 06:34:31

回答

1

有幾件事情你應該在此查詢更新:

  1. 在select子句中使用表別名而不是完整的表名。 !
    objListCustomerFilter [Constants.IS_CALLING_CUSTOMER] = NULL ||:

  2. TBL_Customer_Contact_Master基於條件被接合 objListCustomerFilter [Constants.IS_PAYEE_CUSTOMER]!= null || Convert.ToInt32(objListCustomerFilter [「CUTM_Customer_Type_ID」]) == 120)如果這是真的,那麼有Left Join其他Inner join

  3. 所以更新語句爲:

    串strFromClause = 「FROM TBL_Company_Master中醫」 + 「左加入TBL_Service_Center_Master TSC上 TCM.CMPM_Service_Center_ID = TSC.SCRM_Service_Center_ID」 + 「左加入TBL_City_Location_Master TCL在 TCM.CMPM_City_Location_ID = TCL.CLIM_City_ID「+ 」Left加入TBL_Country_Master TC on TCM.CMPM_Country_ID = TC.CRIM_CountryID「;

  4. 更新條件1爲:!

    如果(objListCustomerFilter [Constants.IS_CALLING_CUSTOMER] = NULL || objListCustomerFilter [Constants.IS_PAYEE_CUSTOMER] = NULL || Convert.ToInt32( objListCustomerFilter [ 「CUTM_Customer_Type_ID」] )== 120)

    strFromClause + = 「左加入TBL_Customer_Contact_Master TCCM上TCM.CMPM_Company_ID = TCCM.CNTM_Customer_ID」; else strFromClause + =「TCM.CMPM_Company_ID上的內部連接TBL_Customer_Contact_Master TCCM = TCCM.CNTM_Customer_ID」;

  5. 然後,當更新條件2: 如果(!objListCustomerFilter [ 「CUTM_Customer_Type_ID」] =空& & Convert.ToString (objListCustomerFilter [ 「CUTM_Customer_Type_ID」])= 「」){

    strFromClause + = 「左加入上CUTM_Customer_ID = CMPM_Company_ID AND CUTM_Customer_Type_ID = TBL_Customer_Type_Mapping 」+ Convert.ToString(objListCustomerFilter [「 CUTM_Customer_Type_ID」];}

  6. 條件3爲:

    如果((objListCustomerFilter [Constants.IS_CALLING_CUSTOMER]!= NULL)||(objListCustomerFilter [Constants.IS_END_USER]!= NULL)) strFromClause + =「左加入TBL_Company_Payment_Terms TCPT 在TCM.CMPM_Company_ID = TCPT.CMPT_Company_ID和TCPT .CMPT_Default ='Y'「; else strFromClause + =「內部加入TBL_Company_Payment_Terms TCPT On TCM.CMPM_Company_ID = TCPT.CMPT_Company_ID AND TCPT.CMPT_Default ='Y'」;

我可能錯過了幾個逗號和分號,但它應該給你一個想法,可能會丟失一些東西。希望這可以幫助!!!

+0

謝謝。我猜,我做了一些愚蠢的事情。我只是在if語句中刪除了On條件。我嘗試用內連接替換左連接,並且輸出是相同的。 雖然,我很困惑,爲什麼我得到更少的行。如果我使用左連接,而不是內連接,我應該收到更多的行,對吧? – jitendragarg 2014-09-26 07:27:19

+0

順便說一句,如果我添加On條件,我必須刪除和子句。只是提及它以備將來參考。 – jitendragarg 2014-09-26 07:28:41

+0

我可以建議你創建每個'strFromClause'條件一個接一個,即一次添加一個,然後調試這兩個解決方案..你會知道哪個條件會破壞你的代碼.. – Deepshikha 2014-09-26 07:32:17

0

該訂單在左外連接無關緊要,請保持放心,這不會導致它。我的猜測是,如果統計數據可能導致結果的差異。 如果您可以共享表格數據或輸出,則可以計算出來。

0

區別在於額外的where子句過濾器如何應用於外連接表中的列。

有了這個:

select * 
from a 
    left outer join b on a.id = b.id 
where 
    b.other_col = 'test' 

結果將只包含其中b找到行,並在bother_col列具有價值test行。

相較於這一點:

select * 
from a, b 
where 
    a.id *= b.id 
    and b.other_col = 'test' 

這會找到a所有行。並且它將包括來自b的列,其中b.other_col = 'test'

因此,採取第二個查詢,並將其與左轉換爲一個連接,下面的人會給出相同的輸出:

-- 1. 

select * 
from a 
    left outer join b on a.id = b.id and b.other_col = 'test' 

-- 2. 

select * 
from a 
    left outer join 
    (
     select * 
     from b 
     where other_col = 'test' 
    ) as b on a.id = b.id