mysql> CREATE TABLE `json_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`info` json NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
mysql> select * from json_table ;
+----+------------------------------+
| id | info |
+----+------------------------------+
| 1 | {"age": 24, "name": "lucy"} |
| 2 | {"age": 20, "name": "lili"} |
| 3 | {"age": 25, "name": "curry"} |
+----+------------------------------+
3 rows in set (0.00 sec)
JSON查询
提取json字段:json列->'$.键' 或 JSON_EXTRACT(json列 , '$.键')
去掉json字段双引号:JSON_UNQOUTE() 或者 json列->>'$.键'
提取JSON 字段的表达式可以用于SELECT查询列表 ,WHERE/HAVING , ORDER/GROUP BY语句中,JSON 中的元素搜索也是严格区分变量类型
json不同于字符串,不能当作字符串直接做比较,通过CAST()将字符串转换成JSON形式,再进行比较,后面举栗
举个栗子:
json列->'$.键' 查询:
mysql> select id, info->'$.name' as name from json_table ;
+----+---------+
| id | name |
+----+---------+
| 1 | "lucy" |
| 2 | "lili" |
| 3 | "curry" |
+----+---------+
3 rows in set (0.00 sec)
mysql> select id, info->'$.name' as name from json_table where info->'$.age' >=24;
+----+---------+
| id | name |
+----+---------+
| 1 | "lucy" |
| 3 | "curry" |
+----+---------+
2 rows in set (0.00 sec)
JSON_EXTRACT(json列 , '$.键')查询:
mysql> select id, json_extract(info, '$.name') as name from json_table ;
+----+---------+
| id | name |
+----+---------+
| 1 | "lucy" |
| 2 | "lili" |
| 3 | "curry" |
+----+---------+
3 rows in set (0.05 sec)
mysql> select id, json_extract(info, '$.name') as name from json_table where json_extract(info, '$.age') >= 24;
+----+---------+
| id | name |
+----+---------+
| 1 | "lucy" |
| 3 | "curry" |
+----+---------+
2 rows in set (0.00 sec)
JSON_UNQOUTE()方法举栗:
mysql> select id, json_unquote(json_extract(info, '$.name')) as name from json_table ;
+----+-------+
| id | name |
+----+-------+
| 1 | lucy |
| 2 | lili |
| 3 | curry |
+----+-------+
3 rows in set (0.00 sec)
mysql> select id, info->>'$.name' as name from json_table ;
+----+-------+
| id | name |
+----+-------+
| 1 | lucy |
| 2 | lili |
| 3 | curry |
+----+-------+
3 rows in set (0.00 sec)
JSON字段与字符串比较举栗:
mysql> select * from json_table where info = '{"age": 25, "name": "curry"}';
Empty set (0.00 sec)
mysql> select * from json_table where info = cast('{"age": 25, "name": "curry"}' as JSON);
+----+------------------------------+
| id | info |
+----+------------------------------+
| 3 | {"age": 25, "name": "curry"} |
+----+------------------------------+
1 row in set (0.00 sec)
虚拟列
值得一提的是,可以通过虚拟列对JSON类型的指定属性进行快速查询,
栗如,创建索引:
mysql> alter table json_table add name varchar(20) generated always as (info->'$.name') virtual ;
Query OK, 0 rows affected (0.35 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from json_table ;
+----+------------------------------+---------+
| id | info | name |
+----+------------------------------+---------+
| 1 | {"age": 24, "name": "lucy"} | "lucy" |
| 2 | {"age": 20, "name": "lili"} | "lili" |
| 3 | {"age": 25, "name": "curry"} | "curry" |
| 4 | {"age": 24, "name": "tom"} | "tom" |
| 5 | {"age": 24, "name": "jurry"} | "jurry" |
| 6 | {"age": 30, "name": "tmry"} | "tmry" |
+----+------------------------------+---------+
6 rows in set (0.00 sec)
mysql> select * from json_table where name=""tom"";
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tom""' at line 1
mysql> select * from json_table where name="\"tom\"";
+----+----------------------------+-------+
| id | info | name |
+----+----------------------------+-------+
| 4 | {"age": 24, "name": "tom"} | "tom" |
+----+----------------------------+-------+
1 row in set (0.00 sec)
mysql> alter table json_table add index name_idx(name) ;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table json_table \G
*************************** 1. row ***************************
Table: json_table
Create Table: CREATE TABLE `json_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`info` json NOT NULL,
`name` varchar(20) GENERATED ALWAYS AS (json_extract(`info`,'$.name')) VIRTUAL,
PRIMARY KEY (`id`),
KEY `name_idx` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> alter table json_table rename index name_idx to idx_name ;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table json_table \G
*************************** 1. row ***************************
Table: json_table
Create Table: CREATE TABLE `json_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`info` json NOT NULL,
`name` varchar(20) GENERATED ALWAYS AS (json_extract(`info`,'$.name')) VIRTUAL,
PRIMARY KEY (`id`),
KEY `idx_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)