+-----+--------+------------------------------------------+------+
| PID | cat_id | product_name | unit |
+-----+--------+------------------------------------------+------+
| 19 | 1 | CASHEW NUT | KG |
+-----+--------+------------------------------------------+------+
我需要在select查询中查看此Unicode中的原始文本,此Unicode包括英语和其他语言。
答案 0 :(得分:0)
以下仅适用于提供的示例,但我想您可以根据需要进行调整:
SELECT CONCAT(CHAR(RIGHT(SUBSTRING_INDEX('CASHEW NUT','&#',2),2)),
CHAR(RIGHT(SUBSTRING_INDEX('CASHEW NUT','&#',3),2)),
CHAR(RIGHT(SUBSTRING_INDEX('CASHEW NUT','&#',4),2)),
CHAR(RIGHT(SUBSTRING_INDEX('CASHEW NUT','&#',5),2)),
CHAR(RIGHT(SUBSTRING_INDEX('CASHEW NUT','&#',6),2)),
CHAR(RIGHT(SUBSTRING_INDEX('CASHEW NUT','&#',7),2)),
CHAR(RIGHT(SUBSTRING_INDEX('CASHEW NUT','&#',8),2)),
CHAR(RIGHT(SUBSTRING_INDEX('CASHEW NUT','&#',9),2)),
CHAR(RIGHT(SUBSTRING_INDEX('CASHEW NUT','&#',10),2)),
CHAR(RIGHT(SUBSTRING_INDEX('CASHEW NUT','&#',11),2))
);
结果= CASHEW NUT
答案 1 :(得分:0)
为什么首先将文本存储为HTML代码?当你真正对真实文本感兴趣时,这是一个非常糟糕的主意。您应该更改此内容或决定不对字符串内容的含义感兴趣,即只是存储这些字符串代码,但只能将它们解释为在数据库中。
然而, 可能让MySQL进行转换。你需要一个功能:
CREATE FUNCTION decode_string(encoded_string TEXT)
RETURNS TEXT
BEGIN
DECLARE decoded_string TEXT DEFAULT ''; -- the target string
DECLARE ucodestr TEXT DEFAULT ''; -- a code taken from the source string
DECLARE strlen INT; -- the length of the string to scan
DECLARE strpos INT DEFAULT 1; -- the position in the string to scan
DECLARE chr TEXT; -- a character from the string to scan
-- Convert the encoded string from 'CAS' to '67;65;83;' so every code is followed by a semicolon.
SET encoded_string = REPLACE(CONCAT(SUBSTR(encoded_string, 3), '&#'), '&#', ';');
SET strlen = length(encoded_string);
chrloop: WHILE strpos <= strlen DO
SET chr = substr(encoded_string, strpos, 1);
IF chr = ';' THEN
-- Convert the found code to its unicode character and add this to the output encoded_string.
SET decoded_string = CONCAT(decoded_string, CHAR(ucodestr USING ucs2));
SET ucodestr = '';
ELSE
-- Build the string to hold the complete code number digit by digit.
SET ucodestr = CONCAT(ucodestr, chr);
END IF;
SET strpos = strpos + 1;
END WHILE chrloop;
RETURN decoded_string;
END;
/
然后在SQL中使用此函数,例如:
select decode_string(product_name) as txt
from unicodes_table;