mysql有json型別。MySQL從5.7版本開始,引入了JSON資料型別,可以直接操作json資料;但MySQL5.7版本以下,在表中儲存JSON格式型別的資料,則需要依靠varchar或者text之類的資料型別。
MySQL中的JSON型別
json是一個非常好用的資料型別,在mysql5.7之前人們都是用string來儲存json的,但是這樣有個問題就是不能操作json,5.7以後就可以直接操作json資料了。
{ "bill": [ { "bill": [ { "id": "C81AEAD03F90000142E81B405F6FADC0", "uuid": "cfd872541b3b4507a50c2b2eda6bef28", "billid": "kjdzyljgmzsfzypj", "pageno": [], "billver": "V1", "billname": "新增測試", "fjNumber": "", "trueName": "", "allPageno": [ { "top": 13, "left": 7 } ], "billValue": {}, "isOtherZL": "", "billNumber": "", "fjTMNumber": "" }, { "id": "C81AED69D6400001A07818551D9EBEC0", "uuid": "05d87c8052cd44209c684407d200b7ec", "billid": "opztsfpsgd", "pageno": [], "billver": "V1", "billname": "發票申購", "fjNumber": "", "trueName": "", "allPageno": [ { "top": 13, "left": 7 } ], "isOtherZL": "", "billNumber": "", "fjTMNumber": "" } ], "index": "", "dependBjBill": { "formula": "", "keyView": "" }, "codeCondition": { "formula": "", "keyView": "" }, "billRuleCondition": { "formula": "", "keyView": "" } }, { "bill": [ { "id": "C81AED84903000019B29EAB0196014CE", "uuid": "0d93fe614d09489cbad8fa5a3d160289", "billid": "kjdzcwgwht", "pageno": [], "billver": "V1", "billname": "財務顧", "fjNumber": "", "trueName": "", "allPageno": [ { "top": 39, "left": 7 } ], "isOtherZL": "", "billNumber": "", "fjTMNumber": "" } ], "index": "", "dependBjBill": { "formula": "", "keyView": "" }, "codeCondition": { "formula": "", "keyView": "" }, "billRuleCondition": { "formula": "", "keyView": "" } } ], "questions": [], "relyonCondition": {} }
結構是這樣的,我們要查詢最底層uuid!按條件查bill下是一個陣列 裡面的bill還是一個陣列那麼要查最底層的uuid怎麼查呢?
;
個人更喜歡用類似lambda這種方法畢竟這是一個有逼格又好看的操作:
SELECT content_json->'$.bill[*].bill[*].uuid' from b WHERE JSON_CONTAINS(content_json->'$.bill[*].bill[*].uuid' ,'["cfd872541b3b4507a50c2b2eda6bef28"]')
查詢結果
程式碼分析:
content_json->’$.bill[].bill[].uuid’ ;
表示content_json欄位
"$"是必須符號 點代表物件
*代表全部
那麼程式碼意思就是查詢content_json欄位裡面全部bill(第一個)中全部bill(第二個)的uuid組成一個陣列返回;
where後面跟著的條件的意思是 存在某個值 這個值可以是一個也可以是多個
JSON_CONTAINS(欄位,引數);
欄位必須是要查的指定值如 content_json->’$.bill[].bill[].uuid’ ;
引數必須是json型別 string是不行的。
推薦教學:
以上就是mysql有沒有json型別?的詳細內容,更多請關注TW511.COM其它相關文章!