MySql日期字符串ORDER

时间:2014-11-02 17:04:49

标签: mysql

*我已经编辑了这篇文章(问题是'%d。%m。%Y%h:%i:%s'就像一半)

但是,我还是不明白:/
这工作正常,并按照'desc'顺序选择:

SELECT monitor.data_activity FROM monitor, player WHERE monitor.player_id = player.id AND monitor.player_id = 1
      AND (STR_TO_DATE(monitor.data_activity, '%d.%m.%Y') BETWEEN '2014-10-31' AND '2014-11-01') 
      ORDER BY UNIX_TIMESTAMP(STR_TO_DATE(monitor.data_activity, '%d.%m.%Y %h:%i:%s')) DESC;

但如果我将(AND monitor.player_id = 1)更改为(AND monitor.cord ='1:1:1'),其中cord是varchar10

SELECT monitor.data_activity FROM monitor, player WHERE monitor.player_id = player.id AND monitor.cord = '1:1:1'
      AND (STR_TO_DATE(monitor.data_activity, '%d.%m.%Y') BETWEEN '2014-10-31' AND '2014-11-01') 
      ORDER BY UNIX_TIMESTAMP(STR_TO_DATE(monitor.data_activity, '%d.%m.%Y %h:%i:%s')) DESC;

它打破了我的ORDER BY:

31.10.2014 20:35:27   
01.11.2014 18:20:01   
31.10.2014 00:55:43   
31.10.2014 14:41:17   
31.10.2014 21:07:24    
01.11.2014 19:05:10    

1 个答案:

答案 0 :(得分:1)

这是巧合。两个order by表达式仅按日期排序。在一种情况下,它恰好也按正确的顺序排列时间。你可以使用:

order by STR_TO_DATE(monitor.data_activity, '%d.%m.%Y %H:%i:%s')