从 MySQL 5.7.8 开始,MySQL 支持原生的 JSON 数据类型。
创建 JSON
类似 varchar,设置 JSON 主要将字段的 type 是 json, 不能设置长度,可以是 NULL 但不能有默认值。
mysql> CREATE TABLE lnmp (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`category` JSON,
`tags` JSON,
PRIMARY KEY (`id`)
);
mysql> DESC lnmp;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| category | json | YES | | NULL | |
| tags | json | YES | | NULL | |
+----------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
这样 JSON 的字段就创建好了。
插入 JSON
就是插入 json 格式的字符串,可以是对象的形式,也可以是数组的形式
mysql> INSERT INTO `lnmp` (category, tags) VALUES ('{"id": 1, "name": "lnmp.cn"}', '[1, 2, 3]');
Query OK, 1 row affected (0.01 sec)
MySQL 也有专门的函数 JSON_OBJECT,JSON_ARRAY 生成 json 格式的数据
mysql> INSERT INTO `lnmp` (category, tags) VALUES (JSON_OBJECT("id", 2, "name", "php.net"), JSON_ARRAY(1, 3, 5));
Query OK, 1 row affected (0.00 sec)
查看插入的数据
mysql> SELECT * FROM lnmp;
+----+------------------------------+-----------+
| id | category | tags |
+----+------------------------------+-----------+
| 1 | {"id": 1, "name": "lnmp.cn"} | [1, 2, 3] |
| 2 | {"id": 2, "name": "php.net"} | [1, 3, 5] |
+----+------------------------------+-----------+
2 rows in set (0.00 sec)
查询 JSON
查询 json 中的数据用 column->path的形式,其中对象类型 path 这样表示 $.path, 而数组类型则是 $[index]
mysql> SELECT id, category->'$.id', category->'$.name', tags->'$[0]', tags->'$[2]' FROM lnmp;
+----+------------------+--------------------+--------------+--------------+
| id | category->'$.id' | category->'$.name' | tags->'$[0]' | tags->'$[2]' |
+----+------------------+--------------------+--------------+--------------+
| 1 | 1 | "lnmp.cn" | 1 | 3 |
| 2 | 2 | "php.net" | 1 | 5 |
+----+------------------+--------------------+--------------+--------------+
2 rows in set (0.00 sec)
可以看到对应字符串类型的 category->'$.name' 中还包含着双引号,这其实并不是想要的结果,可以用 JSON_UNQUOTE 函数将双引号去掉,从 MySQL 5.7.13 起也可以通过这个操作符 ->>这个和 JSON_UNQUOTE 是等价的
mysql> SELECT id, category->'$.name', JSON_UNQUOTE(category->'$.name'), category->>'$.name' FROM lnmp;
+----+--------------------+----------------------------------+---------------------+
| id | category->'$.name' | JSON_UNQUOTE(category->'$.name') | category->>'$.name' |
+----+--------------------+----------------------------------+---------------------+
| 1 | "lnmp.cn" | lnmp.cn | lnmp.cn |
| 2 | "php.net" | php.net | php.net |
+----+--------------------+----------------------------------+---------------------+
2 rows in set (0.00 sec)
下面说下 JSON 作为条件进行搜索。因为 JSON 不同于字符串,所以如果用字符串和 JSON 字段比较,是不会相等的
mysql> SELECT * FROM lnmp WHERE category = '{"id": 1, "name": "lnmp.cn"}';
Empty set (0.00 sec)
这时可以通过 CAST 将字符串转成 JSON 的形式
mysql> SELECT * FROM lnmp WHERE category = CAST('{"id": 1, "name": "lnmp.cn"}' as JSON);
+----+------------------------------+-----------+
| id | category | tags |
+----+------------------------------+-----------+
| 1 | {"id": 1, "name": "lnmp.cn"} | [1, 2, 3] |
+----+------------------------------+-----------+
1 row in set (0.00 sec)
通过 JSON 中的元素进行查询, 对象型的查询同样可以通过 column->path
mysql> SELECT * FROM lnmp WHERE category->'$.name' = 'lnmp.cn';
+----+------------------------------+-----------+
| id | category | tags |
+----+------------------------------+-----------+
| 1 | {"id": 1, "name": "lnmp.cn"} | [1, 2, 3] |
+----+------------------------------+-----------+
1 row in set (0.00 sec)
上面有提到 column->path形式从 select 中查询出来的字符串是包含双引号的,但作为条件这里其实没什么影响,-> 和 ->> 结果是一样的
mysql> SELECT * FROM lnmp WHERE category->>'$.name' = 'lnmp.cn';
+----+------------------------------+-----------+
| id | category | tags |
+----+------------------------------+-----------+
| 1 | {"id": 1, "name": "lnmp.cn"} | [1, 2, 3] |
+----+------------------------------+-----------+
1 row in set (0.00 sec)
要特别注意的是,JSON 中的元素搜索是严格区分变量类型的,比如说整型和字符串是严格区分的
mysql> SELECT * FROM lnmp WHERE category->'$.id' = '1';
Empty set (0.00 sec)
mysql> SELECT * FROM lnmp WHERE category->'$.id' = 1;
+----+------------------------------+-----------+
| id | category | tags |
+----+------------------------------+-----------+
| 1 | {"id": 1, "name": "lnmp.cn"} | [1, 2, 3] |
+----+------------------------------+-----------+
1 row in set (0.00 sec)
可以看到搜索字符串 1 和整型 1 的结果是不一样的。
除了用 column->path的形式搜索,还可以用JSON_CONTAINS 函数,但和 column->path的形式有点相反的是,JSON_CONTAINS 第二个参数是不接受整数的,无论 json 元素是整型还是字符串,否则会出现这个错误
mysql> SELECT * FROM lnmp WHERE JSON_CONTAINS(category, 1, '$.id');
ERROR 3146 (22032): Invalid data type for JSON data in argument 2 to function json_contains; a JSON string or JSON type is required.
这里必须是要字符串 1
mysql> SELECT * FROM lnmp WHERE JSON_CONTAINS(category, '1', '$.id');
+----+------------------------------+-----------+
| id | category | tags |
+----+------------------------------+-----------+
| 1 | {"id": 1, "name": "lnmp.cn"} | [1, 2, 3] |
+----+------------------------------+-----------+
1 row in set (0.01 sec)
对于数组类型的 JSON 的查询,比如说 tags 中包含有 2 的数据,同样要用 JSON_CONTAINS 函数,同样第二个参数也需要是字符串
mysql> SELECT * FROM lnmp WHERE JSON_CONTAINS(tags, '2');
+----+------------------------------+-----------+
| id | category | tags |
+----+------------------------------+-----------+
| 1 | {"id": 1, "name": "lnmp.cn"} | [1, 2, 3] |
+----+------------------------------+-----------+
1 row in set (0.00 sec)
更新 JSON
如果是整个 json 更新的话,和插入时类似的。
mysql> UPDATE lnmp SET tags = '[1, 3, 4]' WHERE id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM lnmp;
+----+------------------------------+-----------+
| id | category | tags |
+----+------------------------------+-----------+
| 1 | {"id": 1, "name": "lnmp.cn"} | [1, 3, 4] |
| 2 | {"id": 2, "name": "php.net"} | [1, 3, 5] |
+----+------------------------------+-----------+
2 rows in set (0.00 sec)
但如果要更新 JSON 下的元素,MySQL 并不支持 column->path的形式
mysql> UPDATE lnmp SET category->'$.name' = 'lnmp', tags->'$[0]' = 2 WHERE id = 1;
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 '->'$.name' = 'lnmp', tags->'$[0]' = 2 WHERE id = 1' at line 1
则可能要用到以下几个函数
JSON_INSERT() 插入新值,但不会覆盖已经存在的值
mysql> UPDATE lnmp SET category = JSON_INSERT(category, '$.name', 'lnmp', '$.url', 'www.lnmp.cn') WHERE id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM lnmp;
+----+----------------------------------------------------+-----------+
| id | category | tags |
+----+----------------------------------------------------+-----------+
| 1 | {"id": 1, "url": "www.lnmp.cn", "name": "lnmp.cn"} | [1, 3, 4] |
| 2 | {"id": 2, "name": "php.net"} | [1, 3, 5] |
+----+----------------------------------------------------+-----------+
2 rows in set (0.00 sec)
可以看到 name 没有被修改,但新元素 url 已经添加进去
JSON_SET() 插入新值,并覆盖已经存在的值
mysql> UPDATE lnmp SET category = JSON_SET(category, '$.host', 'www.lnmp.cn', '$.url', 'http://www.lnmp.cn') WHERE id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM lnmp;
+----+----------------------------------------------------------------------------------+-----------+
| id | category | tags |
+----+----------------------------------------------------------------------------------+-----------+
| 1 | {"id": 1, "url": "http://www.lnmp.cn", "host": "www.lnmp.cn", "name": "lnmp.cn"} | [1, 3, 4] |
| 2 | {"id": 2, "name": "php.net"} | [1, 3, 5] |
+----+----------------------------------------------------------------------------------+-----------+
2 rows in set (0.00 sec)
可以看到 host 已经插入,url 已经被修改
JSON_REPLACE() 只替换存在的值
mysql> UPDATE lnmp SET category = JSON_REPLACE(category, '$.name', 'php', '$.url', 'http://www.php.net') WHERE id = 2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM lnmp;
+----+----------------------------------------------------------------------------------+-----------+
| id | category | tags |
+----+----------------------------------------------------------------------------------+-----------+
| 1 | {"id": 1, "url": "http://www.lnmp.cn", "host": "www.lnmp.cn", "name": "lnmp.cn"} | [1, 3, 4] |
| 2 | {"id": 2, "name": "php"} | [1, 3, 5] |
+----+----------------------------------------------------------------------------------+-----------+
2 rows in set (0.00 sec)
可以看到 name 已经被替换,url 不存在被忽略。
JSON_REMOVE() 删除 JSON 元素
mysql> UPDATE lnmp SET category = JSON_REMOVE(category, '$.url', '$.host') WHERE id = 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM lnmp;
+----+------------------------------+-----------+
| id | category | tags |
+----+------------------------------+-----------+
| 1 | {"id": 1, "name": "lnmp.cn"} | [1, 3, 4] |
| 2 | {"id": 2, "name": "php"} | [1, 3, 5] |
+----+------------------------------+-----------+
2 rows in set (0.00 sec)
MySQL JSON 在 PHP 中的表现
虽然在 MySQL 是个JSON 类型,但实际在 PHP 应用中返回的是 JSON 格式的字符串
array(2) {
[0]=>
array(3) {
["id"]=>
string(1) "1"
["category"]=>
string(28) "{"id": 1, "name": "lnmp.cn"}"
["tags"]=>
string(9) "[1, 3, 4]"
}
[1]=>
array(3) {
["id"]=>
string(1) "2"
["category"]=>
string(24) "{"id": 2, "name": "php"}"
["tags"]=>
string(9) "[1, 3, 5]"
}
}