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影片教學
#以上是mysql有沒有json類型?的詳細內容。更多資訊請關注PHP中文網其他相關文章!