gpt4 book ai didi

Mysql str_to_date 不执行任何操作

转载 作者:行者123 更新时间:2023-11-29 13:02:57 26 4
gpt4 key购买 nike

我的数据库中有一个 varchar 类型的 datecreated 字段。不幸的是,我无法将此字段更改为日期格式,因此我必须在选择数据时执行此操作。我试图根据日期对记录进行排序(以便显示最近的年、月、日、小时、分钟、秒)。字段格式看起来像这样 dd-mm-yyyy hh:mm:ss ,所以在我的查询中,我正在尝试以下操作,但它完全没有排序:

SELECT * FROM submissions ORDER BY str_to_date(datecreated,'%d-%m-%Y %H:%i:%s') DESC

它所做的只是根据 id 字段显示记录...

最佳答案

假设所有 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)

关于Mysql str_to_date 不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23087142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com