如何将 SQL 请求中的文本替换为 ASCII 字符的组合?
How to replace text in SQL request into combination of ASCII characters?
我想要的很简单:在 table 中有一个 TEXT
字段我想将它的每个字符从 ASCII [☺, ☻, ♥, ♦, ♣, ♠, ♂, ♀, ☼]
数组中转换为一个字符,转换很简单as possible: given char as int, mod 9. 这种事情在 MySQL 中可能吗?如何做?
这只是一些指南,我只知道 mysql 基础知识。
您需要 set
个变量
执行字符串函数 substring
、ascii
、concat
和一个 while
循环
我不知道你是如何处理 mysql 中那个奇怪的 array
的。帮忙说数组不存在
col = '123456';
declare v_max int unsigned default 1000;
declare v_counter int unsigned default 1;
declare myChar varchar(1);
declare result varchar(100) default '';
declare myAscII int;
-- get the lenth of the string
select CHAR_LENGTH(col) INTO v_max;
while v_counter < v_max do
-- get each char ascii
select ASCII(SUBSTRING(col, v_counter , 1)) INTO myAscII;
-- get the array index
set myAscII = myAscII MOD 9;
-- get the array value /*dont know how you handle this*/
set myChar = WEIRD_ARRAY[myAscII]
select CONCAT(result, myChar) into result;
set v_counter = v_counter + 1;
end while;
DROP FUNCTION IF EXISTS wtf;
DELIMITER |
CREATE FUNCTION wtf( str TEXT ) RETURNS TEXT
BEGIN
DECLARE wtf_chars TEXT DEFAULT '☺☻♥♦♣♠♂♀☼';
DECLARE i, len, wtf_len SMALLINT DEFAULT 1;
DECLARE ret TEXT DEFAULT '';
DECLARE c CHAR(1);
SET str = CONVERT(str USING ascii);
SET len = CHAR_LENGTH(str);
SET wtf_len = CHAR_LENGTH(wtf_chars);
REPEAT
BEGIN
SET c = MID(str, i, 1);
SET ret = CONCAT(ret, SUBSTRING(wtf_chars, (MOD(ASCII(c), wtf_len) + 1), 1));
SET i = i + 1;
END;
UNTIL i > len END REPEAT;
RETURN ret;
END |
DELIMITER ;
SELECT wtf(`your_text_field`) FROM `your_table`;
我想要的很简单:在 table 中有一个 TEXT
字段我想将它的每个字符从 ASCII [☺, ☻, ♥, ♦, ♣, ♠, ♂, ♀, ☼]
数组中转换为一个字符,转换很简单as possible: given char as int, mod 9. 这种事情在 MySQL 中可能吗?如何做?
这只是一些指南,我只知道 mysql 基础知识。
您需要 set
个变量
执行字符串函数 substring
、ascii
、concat
和一个 while
循环
我不知道你是如何处理 mysql 中那个奇怪的 array
的。帮忙说数组不存在
col = '123456';
declare v_max int unsigned default 1000;
declare v_counter int unsigned default 1;
declare myChar varchar(1);
declare result varchar(100) default '';
declare myAscII int;
-- get the lenth of the string
select CHAR_LENGTH(col) INTO v_max;
while v_counter < v_max do
-- get each char ascii
select ASCII(SUBSTRING(col, v_counter , 1)) INTO myAscII;
-- get the array index
set myAscII = myAscII MOD 9;
-- get the array value /*dont know how you handle this*/
set myChar = WEIRD_ARRAY[myAscII]
select CONCAT(result, myChar) into result;
set v_counter = v_counter + 1;
end while;
DROP FUNCTION IF EXISTS wtf;
DELIMITER |
CREATE FUNCTION wtf( str TEXT ) RETURNS TEXT
BEGIN
DECLARE wtf_chars TEXT DEFAULT '☺☻♥♦♣♠♂♀☼';
DECLARE i, len, wtf_len SMALLINT DEFAULT 1;
DECLARE ret TEXT DEFAULT '';
DECLARE c CHAR(1);
SET str = CONVERT(str USING ascii);
SET len = CHAR_LENGTH(str);
SET wtf_len = CHAR_LENGTH(wtf_chars);
REPEAT
BEGIN
SET c = MID(str, i, 1);
SET ret = CONCAT(ret, SUBSTRING(wtf_chars, (MOD(ASCII(c), wtf_len) + 1), 1));
SET i = i + 1;
END;
UNTIL i > len END REPEAT;
RETURN ret;
END |
DELIMITER ;
SELECT wtf(`your_text_field`) FROM `your_table`;