连接 2 个查询 SQL +冷聚变

发布于 2024-11-14 15:00:18 字数 4371 浏览 1 评论 0原文

有两个查询,并且无法使它们正常工作,他们的最终产品给出了重复的项目,这里是查询:

第一个查询

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们只是彼此的过ke 2024-11-21 15:00:18

该查询没有错误,它只是显示 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,

我看到这个查询可以显示(其他可能有趣的)数据的另外两种方式:

< 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:

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

or

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

or whichever other ordering you like, say:

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

I see two more ways where this query can show (other possibly interesting) data:

1 Change (in the SELECT list) CRM.* into Count(*) 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 any CP.* and keep only Count(*) AS PartnersCount


If you use GROUP BY, you could also use other aggregate functions, like SUM(), AVG(), MIN(), MAX() if you have fields that can be added (numer fields) or compared (like dates).

少女净妖师 2024-11-21 15:00:18

这与使用 DISTINCT 关键字一样简单吗?

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

另外 - 您应该使用 来避免 SQL 注入漏洞。

Is this as simple as using the DISTINCT keyword?

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

Also - you should use <cfqueryparam /> to avoid SQL injection vulnerabilities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文