datetime列上的orderby问题

时间:2011-08-28 20:53:57

标签: mysql database coldfusion

我有一个数据库,在那一刻,我无法直接访问,使用phpmyadmin(我丢失了密码)。

查询使用group by(如果相关)并且我需要按日期字段排序,我只能假设它是日期时间字段。当我直接输出日期时,它输出这样的日期{ts'2011-08-25 11:15:52'}。我需要按此日期订购。

我已经尝试过了,但它根本不影响订购。

ORDER BY i.createdOn DESC

整个查询都在这里。

    <cfquery name="dbRsResults1" datasource="#datasource#" maxrows="#irMaxResults+1#">
    SELECT DISTINCT i.id                AS  id,
                    i.company           AS  company,
                    i.insolvencyDate    AS  insolvency_date,
                    i.city              AS  city,
                    i.createdOn         AS  createdOn,
                    1                   AS  rank_id,
                    t.entryCopy         AS  insolvency_type,
                    i.businessNature    AS  business_nature,
                    'I'                 AS  i_type
    FROM            insolvencies i,
                    <!---practitioners p,--->
                    lookupcopy t
    WHERE           i.checked           =   1                   AND
                    t.id                =   i.insolvencyType    <!---AND
                    p.id                =   i.practitioner--->
                    <!---<cfif isUseDates is "Y">--->
                        AND i.insolvencyDate    >=  #isStartDate#       AND
                        i.insolvencyDate    <=  #isEndDate#
                    <!---</cfif>--->
                    <cfif isType neq 0>
                        AND i.insolvencyType    =   #isType#
                    </cfif>
                    <cfif isRegion  gt 0>
                        AND i.region    =   #isRegion#
                    </cfif>
                    <cfif isCategory gt 0>
                        AND i.businessCategory  =   #isCategory#
                    </cfif>
                    <cfif termsCount gt 0>
                        AND (
                        <cfloop from="1" to="#termsCount#" index="i">
                            (LOWER(i.company)       LIKE    '%#trim(aTerms[i])#%'   OR
                            LOWER(i.businessNAture)     LIKE    '%#trim(aTerms[i])#%')
                            <cfif i LT termscount>
                            OR
                            </cfif>
                        </cfloop>
                        )
                    </cfif>
        GROUP BY        id
                <cfif isSearchByDate is "Y">
                    ORDER BY i.createdOn DESC
                </cfif>
</cfquery>

2 个答案:

答案 0 :(得分:3)

当您使用GROUP BY时,顾名思义,您将生成新行,这些行是一个或多个物理行的组合。如果按原始行中的值排序,则不会获得任何有用的排序顺序。一个例子:

Name    Country Age
======= ======= ===
Abe     UK      20
Bill    France  30
Charles France  25
David   UK      45
Edward  UK      33

如果我们跑:

SELECT Country, MIN(age) AS min_age
FROM people
GROUP BY Country

......我们得到这样的东西:

Country min_age
======= =======
UK      20
France  25

......按名称订购没有意义。

答案 1 :(得分:1)

如果您的查询未按createdOn分组,则您的order by子句将无效。

您需要按所选列的顺序排序。