在 MySQL 8.x 中,字符串函数 FROM_BASE64(str) returns 产生十六进制形式而不是字符串
In MySQL 8.x the string function FROM_BASE64(str) returns result in hexadecimal form instead of a string
我有以下疑问 -
SELECT "Abc" AS result;
+--------+
| result |
+--------+
| Abc |
+--------+
SELECT TO_BASE64("Abc") AS result;
+--------+
| result |
+--------+
| QWJj |
+--------+
SELECT FROM_BASE64(TO_BASE64("Abc")) AS result;
+----------------+
| result |
+----------------+
| 0x416263 |
+----------------+
--binary-as-hex mysql 开发站点中的页面显示 -
To disable hexadecimal notation, use --skip-binary-as-hex
我尝试了以下但出现错误 -
mysql> SELECT FROM_BASE64(TO_BASE64("Abc")) --skip-binary-as-hex AS result;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as-hex AS result' at line 1
mysql> SELECT FROM_BASE64(TO_BASE64("Abc") --skip-binary-as-hex) AS result;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as-hex) AS result' at line 1
mysql> SELECT FROM_BASE64(TO_BASE64("Abc" --skip-binary-as-hex)) AS result;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as-hex)) AS result' at line 1
在同一页上,他们说在 CHAR()
和 CONVERT()
函数的情况下,我如何使用 USING utf8mb4
子句来获得结果,但他们没有说明任何关于 FROM_BASE64()
函数。不过,我试过了,但出现了错误-
SELECT FROM_BASE64(TO_BASE64("Abc") USING utf8mb4) AS result;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'USING utf8mb4) AS result' at line 1
我尝试了 UNHEX()
函数 -
ELECT UNHEX(FROM_BASE64(TO_BASE64("Abc"))) AS result;
+----------------+
| result |
+----------------+
| 0x0ABC |
+----------------+
很明显这不是我们想要的结果,而且都是大写的。这里 LCASE()
不会很好用,因为它会将每个单词都变成小写。
甚至,这个结果不是字符串形式,从 -
可以看出
SELECT SUBSTRING(UNHEX(FROM_BASE64(TO_BASE64("Abc"))) FROM 4) AS result;
+----------------+
| result |
+----------------+
| 0x |
+----------------+
所以唯一的选择似乎是禁用 -binary-as-hex
但是我找不到办法做到这一点。
这里因为在 Whosebug 中有类似的问题 -
'FROM_BASE64' Function Returns Hex Value
但它在 MySQL 版本 5.6.14 上。我正在使用 MySQL 版本 8.0.27 -
mysql --version
mysql Ver 8.0.27 for Linux on x86_64 (MySQL Community Server - GPL)
所以我的情况不同。
当您从 shell 提示符打开命令时,--skip-binary-as-hex 选项将用作 mysql 命令的选项。它不是在 SQL 语法中使用的选项。参见 https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html#option_mysql_binary-as-hex
也就是说,即使启用了二进制十六进制,您也可以将二进制转换为字符串:
mysql> SELECT FROM_BASE64(TO_BASE64("Abc")) AS result;
+----------------+
| result |
+----------------+
| 0x416263 |
+----------------+
1 row in set (0.00 sec)
mysql> SELECT CONVERT(FROM_BASE64(TO_BASE64("Abc")) USING utf8mb4) AS result;
+--------+
| result |
+--------+
| Abc |
+--------+
1 row in set (0.00 sec)
您可能需要使用不同的字符编码。我的是utf8mb4,你的可能不一样
我有以下疑问 -
SELECT "Abc" AS result;
+--------+
| result |
+--------+
| Abc |
+--------+
SELECT TO_BASE64("Abc") AS result;
+--------+
| result |
+--------+
| QWJj |
+--------+
SELECT FROM_BASE64(TO_BASE64("Abc")) AS result;
+----------------+
| result |
+----------------+
| 0x416263 |
+----------------+
--binary-as-hex mysql 开发站点中的页面显示 -
To disable hexadecimal notation, use
--skip-binary-as-hex
我尝试了以下但出现错误 -
mysql> SELECT FROM_BASE64(TO_BASE64("Abc")) --skip-binary-as-hex AS result;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as-hex AS result' at line 1
mysql> SELECT FROM_BASE64(TO_BASE64("Abc") --skip-binary-as-hex) AS result;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as-hex) AS result' at line 1
mysql> SELECT FROM_BASE64(TO_BASE64("Abc" --skip-binary-as-hex)) AS result;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as-hex)) AS result' at line 1
在同一页上,他们说在 CHAR()
和 CONVERT()
函数的情况下,我如何使用 USING utf8mb4
子句来获得结果,但他们没有说明任何关于 FROM_BASE64()
函数。不过,我试过了,但出现了错误-
SELECT FROM_BASE64(TO_BASE64("Abc") USING utf8mb4) AS result;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'USING utf8mb4) AS result' at line 1
我尝试了 UNHEX()
函数 -
ELECT UNHEX(FROM_BASE64(TO_BASE64("Abc"))) AS result;
+----------------+
| result |
+----------------+
| 0x0ABC |
+----------------+
很明显这不是我们想要的结果,而且都是大写的。这里 LCASE()
不会很好用,因为它会将每个单词都变成小写。
甚至,这个结果不是字符串形式,从 -
可以看出SELECT SUBSTRING(UNHEX(FROM_BASE64(TO_BASE64("Abc"))) FROM 4) AS result;
+----------------+
| result |
+----------------+
| 0x |
+----------------+
所以唯一的选择似乎是禁用 -binary-as-hex
但是我找不到办法做到这一点。
这里因为在 Whosebug 中有类似的问题 -
'FROM_BASE64' Function Returns Hex Value
但它在 MySQL 版本 5.6.14 上。我正在使用 MySQL 版本 8.0.27 -
mysql --version
mysql Ver 8.0.27 for Linux on x86_64 (MySQL Community Server - GPL)
所以我的情况不同。
当您从 shell 提示符打开命令时,--skip-binary-as-hex 选项将用作 mysql 命令的选项。它不是在 SQL 语法中使用的选项。参见 https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html#option_mysql_binary-as-hex
也就是说,即使启用了二进制十六进制,您也可以将二进制转换为字符串:
mysql> SELECT FROM_BASE64(TO_BASE64("Abc")) AS result;
+----------------+
| result |
+----------------+
| 0x416263 |
+----------------+
1 row in set (0.00 sec)
mysql> SELECT CONVERT(FROM_BASE64(TO_BASE64("Abc")) USING utf8mb4) AS result;
+--------+
| result |
+--------+
| Abc |
+--------+
1 row in set (0.00 sec)
您可能需要使用不同的字符编码。我的是utf8mb4,你的可能不一样