gpt4 book ai didi

MySQL : strange LENGTH() behaviour on utf8 string

转载 作者:IT老高 更新时间:2023-10-29 00:02:34 27 4
gpt4 key购买 nike

我正在对请求生成器进行单元测试,但我遇到了 LENGTH 函数的问题。

我有 2 个请求:

SHOW VARIABLES LIKE '%character%'

返回以下结果:

array(8) {
[0] =>
array(2) {
'Variable_name' =>
string(20) "character_set_client"
'Value' =>
string(4) "utf8"
}
[1] =>
array(2) {
'Variable_name' =>
string(24) "character_set_connection"
'Value' =>
string(4) "utf8"
}
[2] =>
array(2) {
'Variable_name' =>
string(22) "character_set_database"
'Value' =>
string(6) "latin1"
}
[3] =>
array(2) {
'Variable_name' =>
string(24) "character_set_filesystem"
'Value' =>
string(6) "binary"
}
[4] =>
array(2) {
'Variable_name' =>
string(21) "character_set_results"
'Value' =>
string(4) "utf8"
}
[5] =>
array(2) {
'Variable_name' =>
string(20) "character_set_server"
'Value' =>
string(4) "utf8"
}
[6] =>
array(2) {
'Variable_name' =>
string(20) "character_set_system"
'Value' =>
string(4) "utf8"
}
[7] =>
array(2) {
'Variable_name' =>
string(18) "character_sets_dir"
'Value' =>
string(26) "/usr/share/mysql/charsets/"
}
}

我的第二个请求是:

SELECT LENGTH('重庆') as len

它返回 6 而不是 2。

这里有什么问题吗?我的字符集参数看起来不错。

最佳答案

我在 MySQL documentation 中找到了答案:

LENGTH 函数计算字节:

mysql> SELECT LENGTH('重庆') ;
+------------------+
| LENGTH('重庆') |
+------------------+
| 6 |
+------------------+
1 row in set (0.00 sec)

CHAR_LENGTH 函数计算字符:

mysql> SELECT CHAR_LENGTH('重庆') ;
+-----------------------+
| CHAR_LENGTH('重庆') |
+-----------------------+
| 2 |
+-----------------------+
1 row in set (0.00 sec)

关于MySQL : strange LENGTH() behaviour on utf8 string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16278898/

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