1

我不明白为什么以下比较在 utf8 (mysql 5.5.27 - dotdeb) 中不起作用。

select '00:00:00' < curtime();

你能解释一下 mysql 如何评估这个表达式,因为我没有看到任何逻辑原因导致它不起作用:

mysql> set names 'utf8';
Query OK, 0 rows affected (0.00 sec)

mysql> select '00:00:00' < curtime();
ERROR 1267 (HY000): Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,NUMERIC) for operation '<'

mysql> set names 'latin1';
Query OK, 0 rows affected (0.00 sec)

mysql> select '00:00:00' < curtime();
+------------------------+
| '00:00:00' < curtime() |
+------------------------+
| 1 |
+------------------------+
1 row in set (0.00 sec

另一个例子,如果比较是按照一定的顺序进行的:

mysql> set names 'utf8';
Query OK, 0 rows affected (0.00 sec)

mysql> select '00:00:00' < curtime();
ERROR 1267 (HY000): Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,NUMERIC) for operation '<'
mysql> select curtime() > '00:00:00';
+------------------------+
| curtime() > '00:00:00' |
+------------------------+
| 1 |
+------------------------+
1 row in set (0.00 sec)

utf8 中的排序规则和字符变量:

mysql> show variables like 'collation%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)

mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

这适用于 MariaDb (5.5.25-MariaDB)

MariaDB [(none)]> set names 'utf8';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select '00:00:00' < curtime();
+------------------------+
| '00:00:00' < curtime() |
+------------------------+
|                      1 |
+------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

MariaDB [(none)]> SHOW VARIABLES LIKE 'collation%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
4

2 回答 2

3

您正在尝试比较不同的数据类型 - 字符串和时间,在这种情况下 MySQL 使用类型转换并引发错误。尝试使用 STR_TO_DATE 函数将字符串转换为 TIME 并比较两个相同的类型:

SET NAMES 'utf8';
SELECT STR_TO_DATE('00:00:00', '%H:%i:%s') < CURTIME();

此处的更多信息 -表达式评估中的类型转换

于 2012-08-20T09:51:12.417 回答
0

检查此解决方案,它的工作原理。您需要将价值转化为时间。

例如 1 -

select cast('00:00:00' as time) < curtime();

例如 2 -

CURTIME()<= ADDTIME(time_column,'-00:15:00')
于 2017-02-27T11:32:11.800 回答