我的数据库中有一个datecreated字段,其类型为varchar。不幸的是我无法将此字段更改为日期格式,因此在选择数据时我必须这样做。我正在尝试根据日期对记录进行排序(这样最新的一年然后是一个月然后一天然后是小时然后是分钟然后显示第二个)。字段格式看起来像这个dd-mm-yyyy hh:mm:ss,所以在我的查询中,我尝试以下内容,但它完全没有排序:
SELECT * FROM submissions ORDER BY str_to_date(datecreated,'%d-%m-%Y %H:%i:%s') DESC
它只是根据id字段显示记录......
答案 0 :(得分:1)
假设所有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)