美文网首页
mysql查询某一字段中某一字符串出现的次数

mysql查询某一字段中某一字符串出现的次数

作者: 一曲无忆 | 来源:发表于2019-11-08 15:19 被阅读0次

    当前有一数据表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
    

    相关文章

      网友评论

          本文标题:mysql查询某一字段中某一字符串出现的次数

          本文链接:https://www.haomeiwen.com/subject/vttwbctx.html