mysql json類型


參考了https://www.cnblogs.com/captainad/p/11176127.html, 

最近查數據庫時,遇到了下面這樣的,

 

 

 select 字段名 from 表 where data='{}'怎么都查不出來 ,突然注意到它的字段類型原來是json類型。

json 數據類型是mysql 5.7之后引入的 

新建:json類型創建和其他類型差不多,跳過.

插入:INSERT into Student(content) VALUES ('{"name":"zhangsan","age":22}');

更新:update Student set content = '{"name":"xiaoming","age":19,"height":"1.82"}' where id =2 ; 更新也差不多

增加json數據類型的好處:

1.保證了JSON數據類型的強校驗、

2。MySQL同時提供了一組操作JSON類型數據的內置函數、

3。更優化的存儲格式 ,容易讀取

4。基於json格式的特征支持修改特定的值

 

常用json函數:

 查詢sql :查詢json中某個字段值 

 使用 字段->’$.json屬性’進行查詢條件

或者JSON_EXTRACT(字段, "$.json屬性")

  select  * from student where content->'$.name' = "zhangsan"; 

select content->"$.name" from student where id =2;

select JSON_EXTRACT(content, "$.name") from student where id =2;

 

json_keys 查詢json中含有的所有屬性名,並返回數組

SELECT id,json_keys(content) FROM student;

json_set 增加json中屬性

UPDATE student SET content = json_set(content,'$.weight','105') WHERE id = 2;

 json_replace 替換

JSON_DEPTH 深度 

json_length 長度  

 select  JSON_LENGTH(content) from student ;

{"age": 22, "name": "zhangsan"} 2

{"age": 19, "name": "xiaoming", "height": "1.82", "weight": "105"}  4

 select  JSON_DEPTH (content) from student ;  

{"age": 22, "name": "zhangsan"} 2

{"age": 19, "name": "xiaoming", "height": "1.82", "weight": "105"}  2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM