简体   繁体   中英

MySql date-string ORDER

*I've edited this post(the problem with a '%d.%m.%Y %h:%i:%s' was like a half)

Still, I don't get it :/
This is working fine and selecting in right 'desc' order:

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;

But If I change (AND monitor.player_id = 1) to (AND monitor.cord = '1:1:1') where cord is a 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;

And it breaks my 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    

It is coincidence. Both order by expressions are only ordering by the date. In one case, it happens to put the time in the right order as well. You can just use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM