我有以下疑問 -
SELECT "Abc" AS result;
--------
| result |
--------
| Abc |
--------
SELECT TO_BASE64("Abc") AS result;
--------
| result |
--------
| QWJj |
--------
SELECT FROM_BASE64(TO_BASE64("Abc")) AS result;
----------------
| result |
----------------
| 0x416263 |
----------------
mysql 開發站點中的--binary-as-hex頁面說-
要禁用十六進制表示法,請使用
--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
在同一頁面中,他們說我如何使用該USING utf8mb4子句在函式CHAR()和CONVERT()函式的情況下獲得結果,但他們沒有說明任何關于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
但我找不到辦法做到這一點。
在stackoverflow中有類似的問題 -
“FROM_BASE64”函式回傳十六進制值
但它是在 MySQL 版本 5.6.14 上。我正在使用 MySQL 版本 8.0.27 -
mysql --version
mysql Ver 8.0.27 for Linux on x86_64 (MySQL Community Server - GPL)
所以我的情況是不同的。
uj5u.com熱心網友回復:
--skip-binary-as-hex 選項將用作 mysql 命令的選項,當您從 shell 提示符打開它時。它不是在 SQL 語法中使用的選項。見https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html#option_mysql_binary-as-hex
也就是說,即使啟用了 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,但你的可能不同。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/349045.html
標籤:mysql 二进制 base64 十六进制 字符串函数
上一篇:如何選擇所有日期排除今天前30天
