str_to_date在查询中返回null,但它自己很好

时间:2013-06-13 10:22:46

标签: mysql

我正试图按时间排序一些结果。我已经收集了str_to_date是要走的路,但我似乎错误地使用它,我无法确定,但我认为它转换为NULL,然后没有以有意义的方式排序:

mysql> SELECT member_id, result_result, str_to_date('result_result','%i:%s.%f') FROM results WHERE workout_id = '2' ORDER BY str_to_date('result_result','%i:%s.%f') LIMIT 5;
+-----------+---------------+-----------------------------------------+
| member_id | result_result | str_to_date('result_result','%i:%s.%f') |
+-----------+---------------+-----------------------------------------+
|         0 | 1:35.0        | NULL                                    |
|         1 | 1:35.0        | NULL                                    |
|         3 | 1:40          | NULL                                    |
|         4 | 1:37.8        | NULL                                    |
|         7 | 1:27.3        | NULL                                    |
+-----------+---------------+-----------------------------------------+
5 rows in set, 5 warnings (0.00 sec)

但如果我手动执行,两种结果类型似乎转换得很好:

mysql> select str_to_date('1:40','%i:%s.%f');
+--------------------------------+
| str_to_date('1:40','%i:%s.%f') |
+--------------------------------+
| 00:01:40                       |
+--------------------------------+
1 row in set (0.00 sec)

mysql> select str_to_date('1:35.0','%i:%s.%f');
+----------------------------------+
| str_to_date('1:35.0','%i:%s.%f') |
+----------------------------------+
| 00:01:35                         |
+----------------------------------+
1 row in set (0.00 sec)

任何想法正在发生什么/如何解决它? 谢谢!

1 个答案:

答案 0 :(得分:2)

您不需要函数内的引号。尝试

str_to_date( result_result, '%i:%s.%f' )