有兩個查詢,不能讓他們正常工作,他們的最終產品給人重複的項目,這裏是查詢:加入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>
如果'COMPANY_REMAINDER_MONEY'包含每個公司ID的多個記錄,則需要使用某種聚合。否則公司會在'COMPANY_REMAINDER_MONEY'中爲每個記錄顯示一次,因爲這是sql請求的內容。 – Leigh 2011-06-08 15:26:31
那麼有多個記錄?如果是的話,你想要返回哪一個(最新,最好,..)? – Leigh 2011-06-09 22:17:06
@Leigh你是什麼意思的最新和最偉大的等..? :)對不起,我是一個SQL和Coldfusion的新手,只有多個變量而不是一個,並且不能理解如何避免它 – user775917 2011-06-10 06:49:14