连接 2 个查询 SQL +冷聚变
有两个查询,并且无法使它们正常工作,他们的最终产品给出了重复的项目,这里是查询:
第一个查询
<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 它显示了一些重复的项目,我知道我应该使用诸如 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>
Got two queries, and can't make them work properly, their end product gives the repeated items, here is the queries:
1st Query
<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>
2nd Query:
<cfquery name="get_money" datasource="#dsn2#">
SELECT BAKIYE,BORC,ALACAK FROM COMPANY_REMAINDER_MONEY WHERE COMPANY_ID = #company_id#
</cfquery>
when i try to define the for example C.COMPANY_ID=CRM.COMPANY_ID it shows some repeated items, i know that i should use smth like JOIN and other stuff, but can't understand how to do it :) need help, and thank you all for help!
here's the joined query, still repeated variables:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该查询没有错误,它只是显示 COMPANY_PARTNER 和 COMPANY_CONSUMER_DOMAINS 的所有(有效)组合。如果你想要这样,你可以在最后添加一个
ORDER BY
:或者
或者你喜欢的任何其他排序,说:
我看到这个查询可以显示(其他可能有趣的)数据的另外两种方式:
< strong>1 将(在 SELECT 列表中)
CRM.*
更改为Count(*) AS ConsumerDomainsCount
并添加
GROUP BY CP.COMPANY_ID, CP.PARTNER_ID 位于末尾。
2 镜像操作,
GROUP BY CRM.COMPANY_ID、CRM.DOMAINID
并通过删除任何CP.*
并仅保留来更改 SELECT 列表>Count(*) AS PartnersCount
如果您使用
GROUP BY
,您还可以使用其他聚合函数,例如SUM()
、AVG() ,
MIN()
、MAX()
如果您有可以添加的字段(数字字段)或比较(如日期)。The query is not wrong, it just shows all (valid) combinations of COMPANY_PARTNERs and COMPANY_CONSUMER_DOMAINS. if you want that, you can add an
ORDER BY
in the end:or
or whichever other ordering you like, say:
I see two more ways where this query can show (other possibly interesting) data:
1 Change (in the SELECT list)
CRM.*
intoCount(*) AS ConsumerDomainsCount
and add a
GROUP BY CP.COMPANY_ID, CP.PARTNER_ID
at the end.2 Mirror operation,
GROUP BY CRM.COMPANY_ID, CRM.DOMAINID
and change the SELECT list by removing anyCP.*
and keep onlyCount(*) AS PartnersCount
If you use
GROUP BY
, you could also use other aggregate functions, likeSUM()
,AVG()
,MIN()
,MAX()
if you have fields that can be added (numer fields) or compared (like dates).这与使用
DISTINCT
关键字一样简单吗?另外 - 您应该使用
来避免 SQL 注入漏洞。Is this as simple as using the
DISTINCT
keyword?Also - you should use
<cfqueryparam />
to avoid SQL injection vulnerabilities.