问题:
当前有一数据表t_user_contact,其中content中是储存了用户上传的JSON格式的联系人数据,当我们需要知道某个用户有多少个联系人数量的时候,如何来编写sql查出相应的数据呢?
- t_user_contact数据表
id | name | content |
---|---|---|
1 | 小明 | [{“user_name”: “张三”, “user_mobile”: “123450”}] |
2 | 小红 | [{“user_name”: “李四”, “user_mobile”: “123451”},{“user_name”: “王五”, “user_mobile”: “123452”}] |
解决方法:
我们需要用到length()、 replace() 这两个函数
select name, (length(content) - length(replace(content, 'user_mobile', '')))/length('user_mobile') from t_user_contact
这样一来,我们就能得到每个名字下的联系人个数了。