當(dāng)前有一數(shù)據(jù)表t_user_contact,其中content中是儲(chǔ)存了用戶上傳的JSON格式的聯(lián)系人數(shù)據(jù),當(dāng)我們需要知道某個(gè)用戶有多少個(gè)聯(lián)系人數(shù)量的時(shí)候,如何來(lái)編寫sql查出相應(yīng)的數(shù)據(jù)呢?
簡(jiǎn)化數(shù)據(jù)表
t_user_contact:id, name, content
content中的json格式:[{"user_name": "張三", "user_mobile": "123456"}]
解決方法:
我們需要用到length()、replace()這兩個(gè)函數(shù)
select name, (length(content) - length(replace(content, 'user_mobile', '')))/length('user_mobile') from t_user_contact