假设所有datecreated
值都与您指定的格式匹配,那么该查询应该可以工作。
这是一个示例,显示您的查询在 MySQL 5.6.14 中使用过去 4 年中分布的 10 个随机日期正常工作:
设置:
mysql> create table submissions (
-> id int not null auto_increment primary key,
-> datecreated varchar(20)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
询问:
mysql> SELECT * FROM submissions ORDER BY str_to_date(datecreated,'%d-%m-%Y %H:%i:%s') DESC;
+----+---------------------+
| id | datecreated |
+----+---------------------+
| 3 | 16-11-2013 10:36:23 |
| 10 | 23-03-2013 23:04:52 |
| 4 | 02-08-2012 08:02:59 |
| 1 | 13-06-2012 21:52:34 |
| 7 | 02-10-2011 05:59:49 |
| 9 | 22-07-2011 14:04:19 |
| 2 | 04-07-2011 03:09:08 |
| 8 | 03-06-2011 03:55:04 |
| 6 | 06-01-2011 20:50:50 |
| 5 | 05-01-2011 20:54:16 |
+----+---------------------+
10 rows in set (0.00 sec)