当前有一数据表t_user_contact,其中content中是储存了用户上传的JSON格式的联系人数据,当我们需要知道某个用户有多少个联系人数量的时候,如何来编写sql查出相应的数据呢?
简化数据表
t_user_contact:id, name, content
content中的json格式:[{"user_name": "张三", "user_mobile": "123456"}]
解决方法:
我们需要用到length()、replace()这两个函数
select name, (length(content) - length(replace(content, 'user_mobile', '')))/length('user_mobile') from t_user_contact