gpt4 book ai didi

MySQL 字符串时间问题

转载 作者:行者123 更新时间:2023-11-29 05:15:43 25 4
gpt4 key购买 nike

我将我的时间数据存储在名为“timestart”的 MySQL 行中,我有三行包含值 9:00am10:00am 1:00pm 我正在尝试使用 MySQL 查询中的 STR_TO_DATE 函数按排序顺序提取这些内容。这是我正在使用的语句:

SELECT * FROM db.test WHERE name='Name' and dateusing='2015-10-21' ORDER BY STR_TO_DATE('timeusingstart', '%h:%i%p');

它运行成功,但当我需要时,行会按 10:00am9:00am1:00pm 的顺序返回它们按时间顺序排序。我根据在 this documentation 找到的信息使用了 %h:%i%p .谁能告诉我我在这里做错了什么?非常感谢!

最佳答案

您不是在列上运行 STR_TO_DATE(),而是在字符串上运行它。尝试从 timeusingstart 周围删除引号,它应该可以工作。

顺便说一下,MySQL provides excellent documentation他们的所有功能!

mysql> create table test (test varchar(255));
Query OK, 0 rows affected (0.06 sec)

mysql> insert into test values('9:00am'), ('10:00am'), ('1:00pm');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from test order by str_to_date(test, '%h:%i%p');
+---------+
| test |
+---------+
| 9:00am |
| 10:00am |
| 1:00pm |
+---------+
3 rows in set (0.01 sec)

关于MySQL 字符串时间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33179019/

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