[英]How to replace text in SQL request into combination of ASCII characters?
我想要的很簡單:在表中有一個TEXT
字段,我想將其每個字符從ASCII [☺, ☻, ♥, ♦, ♣, ♠, ♂, ♀, ☼]
數組轉換為一個,轉換為盡可能簡單:將char設置為int,mod9。這在MySQL中是否可行?如何實現?
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`;
這只是一些准則,我只知道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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.