2011-06-08 81 views
0

有兩個查詢,不能讓他們正常工作,他們的最終產品給人重複的項目,這裏是查詢:加入2個查詢SQL +的ColdFusion

1查詢

<cfquery name="get_partner_all" datasource="#dsn#"> 
    SELECT 
     C.COMPANY_ID, 
     C.FULLNAME, 
     CP.MOBILTEL, 
     CP.MOBIL_CODE, 
     CP.IMCAT_ID, 
     CP.COMPANY_PARTNER_TEL, 
     CP.COMPANY_PARTNER_TELCODE, 
     CP.COMPANY_PARTNER_TEL_EXT, 
     CP.MISSION, 
     CP.DEPARTMENT, 
     CP.TITLE, 
     CP.COMPANY_PARTNER_SURNAME, 
     CP.COMPANY_PARTNER_NAME, 
     CP.PARTNER_ID, 
     CP.COMPANY_PARTNER_EMAIL, 
     CP.HOMEPAGE, 
     CP.COUNTY, 
     CP.COUNTRY, 
     CP.COMPANY_PARTNER_ADDRESS, 
     CP.COMPANY_PARTNER_FAX, 
     CP.RECORD_DATE, 
     CP.MEMBER_CODE, 
     CC.COMPANYCAT 
     <cfif isdefined('attributes.report_sort') and attributes.report_sort is 2> 
     ,CCD.SITE_DOMAIN 
     </cfif> 
    FROM 
     COMPANY_PARTNER CP, 
     COMPANY C, 
     COMPANY_CAT CC 
     <cfif isdefined('attributes.report_sort') and attributes.report_sort is 2> 
     ,COMPANY_CONSUMER_DOMAINS CCD 
     </cfif> 
    WHERE 
     C.COMPANY_ID = CP.COMPANY_ID 
     AND C.COMPANYCAT_ID = CC.COMPANYCAT_ID  
    <cfif isDefined('attributes.search_status') and len(attributes.search_status)> 
     AND CP.COMPANY_PARTNER_STATUS = <cfqueryparam cfsqltype="cf_sql_integer" value="#attributes.search_status#"> 
    </cfif> 
    <cfelseif isDefined("attributes.comp_cat") and len(attributes.comp_cat)> 
     AND C.COMPANYCAT_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#attributes.comp_cat#"> 
    </cfif> 
    <cfif isdefined('attributes.report_sort') and attributes.report_sort is 2> 
     AND CP.PARTNER_ID = CCD.PARTNER_ID 
     AND CCD.SITE_DOMAIN = 'www.projedepo.com' 
    </cfif> 
</cfquery> 

第二查詢:

<cfquery name="get_money" datasource="#dsn2#"> 
    SELECT BAKIYE,BORC,ALACAK FROM COMPANY_REMAINDER_MONEY WHERE COMPANY_ID = #company_id# 
</cfquery> 

當我嘗試以限定例如C.COMPANY_ID = CRM.COMPANY_ID它SH ows一些重複的項目,我知道我應該使用像JOIN和其他的東西,但不知道該怎麼做:)需要幫助,並感謝大家的幫助!

這裏的連接查詢,仍反覆變量:

<cfquery name="get_partner_all" datasource="#dsn#"> 
    SELECT DISTINCT 
     C.COMPANY_ID, 
     C.FULLNAME, 
     CP.MOBILTEL, 
     CP.MOBIL_CODE, 
     CP.IMCAT_ID, 
     CP.COMPANY_PARTNER_TEL, 
     CP.COMPANY_PARTNER_TELCODE, 
     CP.COMPANY_PARTNER_TEL_EXT, 
     CP.MISSION, 
     CP.DEPARTMENT, 
     CP.TITLE, 
     CP.COMPANY_PARTNER_SURNAME, 
     CP.COMPANY_PARTNER_NAME, 
     CP.PARTNER_ID, 
     CP.COMPANY_PARTNER_EMAIL, 
     CP.HOMEPAGE, 
     CP.COUNTY, 
     CP.COUNTRY, 
     CP.COMPANY_PARTNER_ADDRESS, 
     CP.COMPANY_PARTNER_FAX, 
     CP.RECORD_DATE, 
     CP.MEMBER_CODE, 
     CC.COMPANYCAT, 
     CRM.* 
     <cfif isdefined('attributes.report_sort') and attributes.report_sort is 2> 
     ,CCD.SITE_DOMAIN 
     </cfif> 
    FROM 
     COMPANY_PARTNER CP, 
     COMPANY C, 
     COMPANY_CAT CC, 
     #dsn2_alias#.COMPANY_REMAINDER_MONEY CRM 
     <cfif isdefined('attributes.report_sort') and attributes.report_sort is 2> 
     ,COMPANY_CONSUMER_DOMAINS CCD 
     </cfif> 
    WHERE 
     C.COMPANY_ID = CP.COMPANY_ID 
     AND C.COMPANYCAT_ID = CC.COMPANYCAT_ID 
     AND C.COMPANY_ID=CRM.COMPANY_ID 
    <cfif isDefined('attributes.search_status') and len(attributes.search_status)> 
     AND CP.COMPANY_PARTNER_STATUS = <cfqueryparam cfsqltype="cf_sql_integer" value="#attributes.search_status#"> 
    </cfif>  
    <cfif isDefined("attributes.cpid") and len(attributes.cpid)> 
     AND C.COMPANY_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#attributes.cpid#"> 
    <cfelseif isDefined("attributes.comp_cat") and len(attributes.comp_cat)> 
     AND C.COMPANYCAT_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#attributes.comp_cat#"> 
    </cfif> 
    <cfif isdefined('attributes.report_sort') and attributes.report_sort is 2> 
     AND CP.PARTNER_ID = CCD.PARTNER_ID 
     AND CCD.SITE_DOMAIN = 'www.projedepo.com' 
    </cfif> 
</cfquery> 
+0

如果'COMPANY_REMAINDER_MONEY'包含每個公司ID的多個記錄,則需要使用某種聚合。否則公司會在'COMPANY_REMAINDER_MONEY'中爲每個記錄顯示一次,因爲這是sql請求的內容。 – Leigh 2011-06-08 15:26:31

+0

那麼有多個記錄?如果是的話,你想要返回哪一個(最新,最好,..)? – Leigh 2011-06-09 22:17:06

+0

@Leigh你是什麼意思的最新和最偉大的等..? :)對不起,我是一個SQL和Coldfusion的新手,只有多個變量而不是一個,並且不能理解如何避免它 – user775917 2011-06-10 06:49:14

回答

0

查詢沒有錯,它只顯示了COMPANY_PARTNER和COMPANY_CONSUMER_DOMAINS的所有(有效)組合。如果你想,你可以在結尾處添加一個ORDER BY

ORDER BY C.COMPANY_ID, 
     CP.PARTNER_ID, 
     CRM.DOMAINID   --- some field in table CRM 

ORDER BY C.COMPANY_ID, 
     CRM.DOMAINID   --- some field in table CRM 
     CP.PARTNER_ID, 

或任何其他命令你喜歡​​,說:

ORDER BY C.COMPANY_NAME 
     C.COMPANY_ID, 
     CP.COUNTRY, 
     CP.COUNTY, 
     CP.TITLE, 
     CP.PARTNER_ID, 

我見此查詢可以顯示的其他兩種方式(其他可能有趣的數據):

變化(在SELECT列表)CRM.*Count(*) AS ConsumerDomainsCount

和在末尾添加一個GROUP BY CP.COMPANY_ID, CP.PARTNER_ID

鏡像操作,GROUP BY CRM.COMPANY_ID, CRM.DOMAINID和刪除任何CP.*更改SELECT列表,只保留Count(*) AS PartnersCount


如果使用GROUP BY,你也可以使用其他聚合函數,如SUM()AVG()MIN()MAX()如果你有可以添加(數字字段)或比較(如日期)的字段。

-1

這是因爲使用DISTINCT關鍵字一樣簡單?

<cfquery name="get_money" datasource="#dsn2#"> 
    SELECT DISTINCT COL1, COL2 ... FROM ... 
</cfquery> 

另外 - 您應該使用<cfqueryparam />以避免SQL注入漏洞。

+0

沒有幫助.. p.s.我沒有減去你的回答:) – user775917 2011-06-10 06:51:19