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

mysql> select json_depth(name) from t1; 

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

mysql> select json_length(name) from t1; 

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

mysql> select json_type(name) from t1; 

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

mysql> SELECT JSON_VALID(‘hello’) a, JSON_VALID(‘「hello」’) b; 

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

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) blog

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

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

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) table

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

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

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'的形式去查詢 ->和->>結果等效

相關文章
相關標籤/搜索