我不明白为什么以下比较在 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)