解決mysql5.7及如下版本沒法解析json字符串的問題

#把key->value(json字符串)拼成sql語句sql

DELIMITER //json

DROP FUNCTION kvJsonData//ide

CREATE FUNCTION kvJsonData(tableName char(20),medKeys text,medValues text,orderId int(11))spa

returns text字符串

BEGINstring

declare num int(11);it

declare count int(2) default 1;table

declare strValues text default "";class

declare strKeys text;sql語句

declare ret text default "";

set strKeys=concat(medKeys,',`order_id`');

set ret=concat("insert into ",tableName,"(",strKeys,") values");

set num= LENGTH(medValues) - LENGTH(REPLACE(medValues, '#', ''))+1;

while count<=num do

if count=1 then

set strValues=concat(SUBSTRING_INDEX(medValues,'#',count),',',orderId);

else

set strValues=SUBSTRING_INDEX(medValues,'#',count);

set strValues=SUBSTRING_INDEX(strValues,'#',-1);

set strValues=concat(strValues,',',orderId);

end if;

set ret=concat(ret,"(",strValues,"),");

set count=count+1;

end while;

set ret=substring(ret,1,LENGTH(ret)-1);

return ret;

END

//

DELIMITER ;

相關文章
相關標籤/搜索