我现在有一个数据库,我无法使用 phpmyadmin 直接访问(我忘记了密码)。
查询使用分组依据(如果相关),我需要按日期字段对其进行排序,我只能假设它是一个日期时间字段。当我直接输出它们时它输出这样的日期{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>
最佳答案
当您使用 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
...按名称排序没有意义。
关于mysql - 日期时间列上的 orderby 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7223880/