我正在使用以下查询返回按日期时间字段排序的行。
SELECT DISTINCT firstname, surname, email, DATE_FORMAT(dateregistered,'%y %b %d') AS dateregistered FROM clients, registrations, courses
WHERE clients.id = registrations.clientid
ORDER BY dateregistered
但它返回
13 Nov 05
14 Apr 01
14 Feb 25
14 Mar 01
而不是:
13 Nov 05
14 Feb 25
14 Mar 01
14 Apr 01
似乎按月按字母顺序排序。
如何在实际日期订购?
答案 0 :(得分:1)
在SELECT子句中使用不同的投影:
... DATE_FORMAT(dateregistered,'%y %b %d') AS formattedDateRegistered
因为您正在将列的列转换为与其本身相同的名称,而是令人困惑的mysql。
所以最终的查询可能是:
SELECT DISTINCT firstname, surname, email,
DATE_FORMAT(dateregistered,'%y %b %d') AS formattedDateregistered
FROM clients, registrations, courses
WHERE clients.id = registrations.clientid
ORDER BY dateregistered
答案 1 :(得分:0)
问题是您已重新格式化该日期列并为其指定了相同的名称。要解决此问题,请在order by clause
:
SELECT DISTINCT firstname, surname, email,
DATE_FORMAT(dateregistered,'%y %b %d') AS dateregistered
FROM clients c join
registrations r
on c.id = r.clientid cross join
courses
ORDER BY r.dateregistered;
请注意,我澄清了查询中的连接。这种结构很奇怪。我认为您错过了courses
上的加入条件。