MySQL 中的 Unicode(十六进制)字符文字
有没有办法在 MySQL 中指定 Unicode 字符文字?
我想用 Ascii 字符替换 Unicode 字符,如下所示:
Update MyTbl Set MyFld = Replace(MyFld, "ẏ", "y")
但我使用的是大多数字体中不可用的更晦涩的字符,因此我希望能够使用 Unicode 字符文字,例如
Update MyTbl Set MyFld = Replace(MyFld, "\u1e8f", "y")
This SQL语句是从 PHP 脚本调用的 - 第一种形式不仅不可读,而且实际上不起作用!
Is there a way to specify Unicode character literals in MySQL?
I want to replace a Unicode character with an Ascii character, something like the following:
Update MyTbl Set MyFld = Replace(MyFld, "ẏ", "y")
But I'm using even more obscure characters which are not available in most fonts, so I want to be able to use Unicode character literals, something like
Update MyTbl Set MyFld = Replace(MyFld, "\u1e8f", "y")
This SQL statement is being invoked from a PHP script - the first form is not only unreadable, but it doesn't actually work!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以指定十六进制文字(甚至二进制文字)使用
0x
、x''
或X''
:但是请注意返回类型是二进制字符串,因此每个字节都会被考虑一个角色。您可以使用
char_length
验证这一点:如果您想要 UTF-8 字符串,您需要使用
转换
:我们可以看到
C2 A2
在 UTF-8 中被视为 1 个字符:此外,您不必担心无效字节,因为
convert
会自动删除它们:可以看出,输出为
0
,因为C1 A2
是无效的 UTF-8 字节序列。You can specify hexadecimal literals (or even binary literals) using
0x
,x''
, orX''
:But be aware that the return type is a binary string, so each and every byte is considered a character. You can verify this with
char_length
:If you want UTF-8 strings instead, you need to use
convert
:And we can see that
C2 A2
is considered 1 character in UTF-8:Also, you don't have to worry about invalid bytes because
convert
will remove them automatically:As can be seen, the output is
0
becauseC1 A2
is an invalid UTF-8 byte sequence.感谢您的建议,但我认为问题出在系统上。
有很多级别需要取消,但据我所知,(至少在这台服务器上)该命令
使 utf-8 处理正常工作,反之则
不然。
在我的环境中,这些是使用 PDO 从 PHP 调用的,这可能会产生什么差异。
无论如何,谢谢!
Thanks for your suggestions, but I think the problem was further back in the system.
There's a lot of levels to unpick, but as far as I can tell, (on this server at least) the command
makes the utf-8 handling work correctly, whereas
doesn't.
In my environment, these are being called from PHP using PDO, for what difference that may make.
Thanks anyway!
您可以使用
十六进制
和unhex
函数,例如:You can use the
hex
andunhex
functions, e.g.:MySQL 字符串语法在此处指定,您可以请注意,没有提供数字转义序列。
但是,当您在 PHP 中嵌入 SQL 时,您可以在 PHP 中计算正确的字节。确保放入 SQL 中的字节实际上与您的 客户端字符集。
The MySQL string syntax is specified here, as you can see, there is no provision for numeric escape sequences.
However, as you are embedding the SQL in PHP, you can compute the right bytes in PHP. Make sure the bytes you put into the SQL actually match your client character set.
还有
char
函数 这将允许您想要的(提供字节数和字符集名称)并获取一个字符。There is also the
char
function that will allow what you wanted (providing byte numbers and a charset name) and getting a char.