如何在mySQL选择查询中将unicode列数据转换为可读文本

时间:2015-09-24 10:57:09

标签: mysql sql database

+-----+--------+------------------------------------------+------+
| PID | cat_id |               product_name               | unit |
+-----+--------+------------------------------------------+------+
|  19 |      1 | &#67&#65&#83&#72&#69&#87&#32&#78&#85&#84 | KG   |
+-----+--------+------------------------------------------+------+

我需要在select查询中查看此Unicode中的原始文本,此Unicode包括英语和其他语言。

2 个答案:

答案 0 :(得分:0)

以下仅适用于提供的示例,但我想您可以根据需要进行调整:

SELECT CONCAT(CHAR(RIGHT(SUBSTRING_INDEX('&#67&#65&#83&#72&#69&#87&#32&#78&#85&#84','&#',2),2)),
    CHAR(RIGHT(SUBSTRING_INDEX('&#67&#65&#83&#72&#69&#87&#32&#78&#85&#84','&#',3),2)),
    CHAR(RIGHT(SUBSTRING_INDEX('&#67&#65&#83&#72&#69&#87&#32&#78&#85&#84','&#',4),2)),
    CHAR(RIGHT(SUBSTRING_INDEX('&#67&#65&#83&#72&#69&#87&#32&#78&#85&#84','&#',5),2)),
    CHAR(RIGHT(SUBSTRING_INDEX('&#67&#65&#83&#72&#69&#87&#32&#78&#85&#84','&#',6),2)),
    CHAR(RIGHT(SUBSTRING_INDEX('&#67&#65&#83&#72&#69&#87&#32&#78&#85&#84','&#',7),2)),
    CHAR(RIGHT(SUBSTRING_INDEX('&#67&#65&#83&#72&#69&#87&#32&#78&#85&#84','&#',8),2)),
    CHAR(RIGHT(SUBSTRING_INDEX('&#67&#65&#83&#72&#69&#87&#32&#78&#85&#84','&#',9),2)),
    CHAR(RIGHT(SUBSTRING_INDEX('&#67&#65&#83&#72&#69&#87&#32&#78&#85&#84','&#',10),2)),
    CHAR(RIGHT(SUBSTRING_INDEX('&#67&#65&#83&#72&#69&#87&#32&#78&#85&#84','&#',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 '&#67&#65&#83' 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;

SQL小提琴:http://www.sqlfiddle.com/#!9/d4afd/1