mysql - character_set_connection 的目的是什么?

标签 mysql character-encoding

只需阅读 Stefan Gehrig 对 Is "SET CHARACTER SET utf8" necessary? 的出色回答,在解释解释和运行查询 w.r.t 的阶段方面比 MySQL 的文档更进一步。字符集和排序规则,但我仍然无法真正理解 character_set_connection 的用途,或者更具体地说,将语句从 character_set_client 转码为 character_set_connection。

为什么不直接使用 character_set_client 进行查询,并在与列值比较时直接从 character_set_client 转码为列的字符集?这个中间阶段的目的是什么?该手册给出了比较文字字符串的示例,但是为什么您首先要这样做,更不用说在 character_set_connection 中与 character_set_client 相对照了?除非我对此的理解(比如“select 'somestr' = 'somestr' from x”)是错误的。

谢谢。

最佳答案

在阅读了答案和文档之后,我只能想到 character_set_connection(和 _collat​​ion)的一个用例:

SELECT "StringA" < "StringB"

character_set_client 只与传输到服务器有关。 character_set_connection(以及不独立于字符集的排序规则)对语句的解释很重要。 “StringA”是否小于“StringB”取决于文字的字符集和排序规则。开发人员可能会选择不同于 character_set_client 的字符集/排序规则。

在实践中,character_set_connection 在大多数情况下并不重要,因为文字与列进行比较,在这种情况下使用列的字符集和排序规则。

如果我错了,请纠正我!

https://dev.mysql.com/doc/refman/5.0/en/charset-connection.html :

What character set should the server translate a statement to after receiving it? For this, the server uses the character_set_connection and collation_connection system variables. It converts statements sent by the client from character_set_client to character_set_connection (except for string literals that have an introducer such as _latin1 or _utf8). collation_connection is important for comparisons of literal strings. For comparisons of strings with column values, collation_connection does not matter because columns have their own collation, which has a higher collation precedence.

关于mysql - character_set_connection 的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16082480/

相关文章:

c# - 如何使用多线程管理连接和命令对象

php - 从 $类别中选择 *

php - 与 Codeigniter 正确使用 'DATE_SUB(now(), INTERVAL 2 MONTH)'

character-encoding - 错误 X3000 : Illegal character in shader file

python - 当密码包含特殊字符时写入连接字符串

mysql - COUNT 正在对行进行意外分组

mysql - 为什么我的存储过程不使用我传递给它的参数? - MySQL

google-apps-script - 使用 Google Apps 脚本从网页提取数据时的字符编码问题

apache-spark - Spark:在HDFS中创建的Paquet文件中日语字母乱码

php - PHP Mailer header 编码问题