MySQL将json对象附加到json对象的数组

时间:2017-08-14 12:24:18

标签: mysql json

在此表格中foo_table我有一列 - foo_ids,其内容如下:
[{"id": "432"}, {"id": "433"}]

我的问题是有没有办法将新的json对象附加到此列? 例如,如果我有这个新对象 - {"id": "554"}我希望我的foo_ids列值变为 -
[{"id": "432"}, {"id": "433"}, {"id": "554"}]
任何想法,如果什么都不存在我怎么插入并添加新的json对象,如果已经存在?

1 个答案:

答案 0 :(得分:4)

您可以使用JSON_ARRAY_APPEND功能,如下所示:

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.19    |
+-----------+
1 row in set (0.00 sec)

mysql> DROP TABLE IF EXISTS `foo_table`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `foo_table` (
    ->   `id` SERIAL,
    ->   `foo_ids` JSON
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO `foo_table` (`foo_ids`)
    -> VALUES (NULL);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT `id`, `foo_ids`
    -> FROM `foo_table`;
+----+---------+
| id | foo_ids |
+----+---------+
|  1 | NULL    |
+----+---------+
1 row in set (0.00 sec)

mysql> UPDATE `foo_table`
    -> SET `foo_ids` = IF(
    ->                     `foo_ids` IS NULL OR
    ->                     JSON_TYPE(`foo_ids`) != 'ARRAY',
    ->                     JSON_ARRAY(),
    ->                     `foo_ids`
    ->                   ),
    ->     `foo_ids` = JSON_ARRAY_APPEND(
    ->                   `foo_ids`,
    ->                   '$',
    ->                   CAST('{"id": "432"}' AS JSON),
    ->                   '$',
    ->                   CAST('{"id": "433"}' AS JSON)
    ->                 )
    -> WHERE `id` = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT `id`, `foo_ids`
    -> FROM `foo_table`;
+----+--------------------------------+
| id | foo_ids                        |
+----+--------------------------------+
|  1 | [{"id": "432"}, {"id": "433"}] |
+----+--------------------------------+
1 row in set (0.00 sec)

mysql> UPDATE `foo_table`
    -> SET `foo_ids` = IF(
    ->                     `foo_ids` IS NULL OR
    ->                     JSON_TYPE(`foo_ids`) != 'ARRAY',
    ->                     JSON_ARRAY(),
    ->                     `foo_ids`
    ->                   ),
    ->     `foo_ids` = JSON_ARRAY_APPEND(
    ->                   `foo_ids`,
    ->                   '$',
    ->                   CAST('{"id": "554"}' AS JSON)
    ->                 )
    -> WHERE `id` = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT `id`, `foo_ids`
    -> FROM `foo_table`;
+----+-----------------------------------------------+
| id | foo_ids                                       |
+----+-----------------------------------------------+
|  1 | [{"id": "432"}, {"id": "433"}, {"id": "554"}] |
+----+-----------------------------------------------+
1 row in set (0.00 sec)

请参阅db-fiddle

<强>更新

还可以在条件:

上使用<=>运算符
mysql> UPDATE `foo_table`
    -> SET `foo_ids` = IF(
    ->                     JSON_TYPE(`foo_ids`) <=> 'ARRAY',
    ->                     `foo_ids`,
    ->                     JSON_ARRAY()
    ->                   ),
    ->     `foo_ids` = JSON_ARRAY_APPEND(
    ->                   `foo_ids`,
    ->                   '$',
    ->                   CAST('{"id": "554"}' AS JSON)
    ->                 )
    -> WHERE `id` = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

请参阅db-fiddle