描述:提取extra中的信息,extra为json对象字符串,格式为{“xxx1”:xxx1,“company”:“company_test”,“xxx2”:xxx2,“xxx3”:xxx3,“xxx4”:xxx4,“remark”:“remark_test”}
需要提取里面的公司信息和备注信息
所用函数:if(判断表达式)、locate(判断字符是否存在)、substring_index(截取字符)
if 使用
if(expr1, expr2, expr3), 若expr1成立,则原式返回expr2, 若expr1不成立,则原式返回expr3。
locate 使用
locate 用于判断某一字符串是否存在,形如locate(字段值,字符串),若字段值中存在改字符串,则返回字符串所在的位置, 若不存在,返回0(当该字符串在字段值的首位时,也会返回0,但在本例中不会出现此情况,fastjson首位为”{“)。
substring_index 使用
形如:substring_index(field, str, index)
说明:substring_index(带截取的字符串,匹配的字符,截取字符的位置)若index为正,则当前函数总字段的左边开始截,若index为负,则当前字段从右边开始截
例如:substring_index(‘123,456,789’, ‘,’ , 1) //结果为123
substring_index(‘123,456,789’, ‘,’, 2) //结果为123,456
substring_index('123,456,789, ‘,’, -1) //结果为789
在本例中结合使用
select
if(locate("company", extra) <> 0, substring_index(substring_index(extra,'company":"',-1), '"',1), "") as "公司信息",
if(locate("remark", extra) <> 0, substring_index(substring_index(extra,'remark":"',-1), '"',1), "") as "备注信息"
from users;