MySql5.7 json查詢


create table t1(name json);
insert into t1 values(’ {
“hello”: “song”,
“num”: 111,
“obj”: { “who”: “me”, “arr”: [ 1, 2, “three” ], “more”:“hey” },
“bool”: true,
“can”: false,
“learning”: null,
“chiness”: “中文”
}' );

mysql> select json_depth(name) from t1; 

返回json文本的深度 輸出結果----4;
1 rows in set (0.02 sec)

mysql> select json_length(name) from t1; 

返回json文本的長度 輸出結果----9
1 rows in set (0.01 sec)

mysql> select json_type(name) from t1; 

返回json值得類型 輸出結果----OBJECT
1 rows in set (0.04 sec)

mysql> SELECT JSON_VALID(‘hello’) a, JSON_VALID(‘“hello”’) b; 

判讀是否是合法的json類型 返回結果:a:0 b:1
1 row in set (0.00 sec)

mysql> select json_keys(name) as a from t1; 

查看存儲的json有哪些key 返回結果:array[
'a'=> [“hello”, “link”, “can”, “num”, “bool”, “learning”, “notLink”, “obj”, “chiness”],
]
1 rows in set (0.05 sec)

mysql> select json_keys(name,’obj’) as a from t1; 

查看obj有哪些key 返回結果:array['a' =>[“more”,“who”,“arr”]]
5rowsinset(0.00sec)

mysql>select jsonsearch(name,′one′,′me′) as a from t1;

查看第一次出現的位置 返回結果:array['“.obj.who”']

mysql> select json_search(name,’all’,’%aaaaa%’) as a from t1; 

查看所有包含aaaaa的位置 返回結果:aaaaa所在的key json_search(name,’all’,’%json%’)
1rows in set (0.00 sec)

mysql> select json_extract(name,’link[0]’) as a from t1; 

抽取值 返回結果:查找的key所對應的value 如:“http://jsonview.com
1 rows in set (0.00 sec)

mysql> select json_extract(name,’obj[0].more[0]’) from t1; 

抽取值 返回結果同上
1 rows in set (0.00 sec)
或者使用下面的方式

mysql> select name,name->’key2’ from t1; 

{“key1”: “value1”, “key2”: “value2”} | “value2” |

mysql> select JSON_ARRAY_APPEND(name,’name’,’xxx’) from t1; 

追加記錄 在指定的json字段追加key-value 參數(字段名,key,value)

mysql> select JSON_REMOVE(name,’name’) from t1; 

刪除json數據 參數(字段名,key)

mysql> select JSON_set(name,’name’,’ooo’) from t1; 

有就替換,沒有就insert

mysql> SELECT JSON_UNQUOTE(JSON_EXTRACT(name, ‘name’)) AS name from t1; 

查詢結果去掉雙引號 返回字段name中的key為name的集合,沒有“”
因為 JSON 不同於字符串,所以如果用字符串和 JSON 字段比較,是不會相等的可以通過
where key->'$.value'的形式去查詢 ->和->>結果等效


免責聲明!

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



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