在sql执行区执行一下语句,即可新增函数,属于参数格式getJsonValue('json包含的字段名',json字符串对应的字段名)
CREATE FUNCTION `getJsonValue`(pKeyName VARCHAR(64),pColName mediumtext) RETURNS varchar(2000) CHARSET utf8 COLLATE utf8_bin
return
substr(pColName,
locate(concat('"',upper(pKeyName),'":"'),upper(pColName))+length(concat('"',upper(pKeyName),'":"')),
case when locate('","',substr(upper(pColName),locate(concat('"',upper(pKeyName),'":"'),upper(pColName))))>0 then (locate('","',substr(upper(pColName),locate(concat('"',upper(pKeyName),'":"'),upper(pColName))+length(concat('"',upper(pKeyName),'":"'))))-1)
else locate('"}',substr(upper(pColName),locate(concat('"',upper(pKeyName),'":"'),upper(pColName))+length(concat('"',upper(pKeyName),'":"'))))-1 end )

本文介绍了一种在SQL中自定义函数的方法,用于从JSON字符串中提取特定字段的值。该函数通过使用substr, locate等函数组合实现,能够处理不同长度的JSON字段。

2267

被折叠的 条评论
为什么被折叠?



