sql从列表中取出一个

时间:2011-06-07 14:05:01

标签: sql coldfusion

这里是情况,我有几家公司,并且在每家公司都有很少的合作伙伴(工人),我需要列出这些公司,列出或不列出的定义因素的问题公司是在合作伙伴的定义,所以当我需要被列入公司时,他们被列出了几次,因为每个都有几个合作伙伴:)我希望它一切都清楚,到目前为止我已经写了一个sql代码:

   <cfquery name="GET_POT_COMPANY" datasource="#DSN#">
                SELECT
                    C.COMPANY_ID,
                    C.MEMBER_CODE,
                    C.FULLNAME,
                    C.PARTNER_ID,
                    C.RECORD_DATE,
                    CC.COMPANYCAT
                    <cfif isdefined('attributes.report_sort2') and attributes.report_sort2 is 1>
                    ,CCD.SITE_DOMAIN
                    </cfif>
                FROM 
                    COMPANY C, 
                    COMPANY_CAT CC
                    <cfif isdefined('attributes.report_sort2') and attributes.report_sort2 is 1>
                    ,COMPANY_CONSUMER_DOMAINS CCD,
                    COMPANY_PARTNER CPA
                    </cfif>
                WHERE
                        C.COMPANYCAT_ID = #attributes.comp_cat# 
                        AND CC.COMPANYCAT_ID = C.COMPANYCAT_ID
                    <cfif isdefined('attributes.report_sort2') and attributes.report_sort2 is 1>
                        AND C.COMPANY_ID = CPA.COMPANY_ID
                        AND CPA.PARTNER_ID = CCD.PARTNER_ID
                        AND CCD.SITE_DOMAIN = 'www.projedepo.com'
                    </cfif>
                ORDER BY 
                    C.RECORD_DATE DESC
            </cfquery>

正如您所看到的,我必须列出合作伙伴可以访问网站projedepo的公司,而是每当公司内的合作伙伴访问该网站时重复公司的名称,因为它对合作伙伴而不是公司进行排序,如何删除这些重复的公司?我看到有一种方法可以实现它虽然删除重复相同的公司ID,但如何做,我不知道,需要帮助,并请大家帮忙!

2 个答案:

答案 0 :(得分:1)

试试这个:

<cfquery name="GET_POT_COMPANY" datasource="#DSN#">
    SELECT
        C.COMPANY_ID,
        C.MEMBER_CODE,
        C.FULLNAME,
        C.PARTNER_ID,
        C.RECORD_DATE,
        CC.COMPANYCAT
          <cfif isdefined('attributes.report_sort2')
            and attributes.report_sort2 is 1>
            , 'www.projedepo.com' AS SITE_DOMAIN
          </cfif>
    FROM 
        COMPANY C, 
        COMPANY_CAT CC
    WHERE
          C.COMPANYCAT_ID = #attributes.comp_cat# 
      AND CC.COMPANYCAT_ID = C.COMPANYCAT_ID
        <cfif isdefined('attributes.report_sort2')
          and attributes.report_sort2 is 1>
          AND EXISTS
            ( SELECT *
              FROM
                COMPANY_CONSUMER_DOMAINS CCD,
                COMPANY_PARTNER CPA
              WHERE C.COMPANY_ID = CPA.COMPANY_ID
                AND CPA.PARTNER_ID = CCD.PARTNER_ID
                AND CCD.SITE_DOMAIN = 'www.projedepo.com'
            )
        </cfif>
    ORDER BY 
        C.RECORD_DATE DESC
</cfquery>

如果您使用JOIN语法而不是(隐式JOIN)WHERE,那也会很好:

<cfquery name="GET_POT_COMPANY" datasource="#DSN#">
    SELECT
        C.COMPANY_ID,
        C.MEMBER_CODE,
        C.FULLNAME,
        C.PARTNER_ID,
        C.RECORD_DATE,
        CC.COMPANYCAT
          <cfif isdefined('attributes.report_sort2')
            and attributes.report_sort2 is 1>
            , 'www.projedepo.com' AS SITE_DOMAIN
          </cfif>
    FROM   COMPANY C
      JOIN COMPANY_CAT CC
          ON CC.COMPANYCAT_ID = C.COMPANYCAT_ID
    WHERE
          C.COMPANYCAT_ID = #attributes.comp_cat# 
        <cfif isdefined('attributes.report_sort2')
          and attributes.report_sort2 is 1>
          AND EXISTS
            ( SELECT *
              FROM   COMPANY_CONSUMER_DOMAINS CCD
                JOIN COMPANY_PARTNER CPA
                    ON CPA.PARTNER_ID = CCD.PARTNER_ID
              WHERE C.COMPANY_ID = CPA.COMPANY_ID
                AND CCD.SITE_DOMAIN = 'www.projedepo.com'
            )
        </cfif>
    ORDER BY C.RECORD_DATE DESC
</cfquery>

答案 1 :(得分:0)

当然SELECT DISTINCT会做你需要的吗?