メインコンテンツまでスキップ
バージョン: V1.4.0

JSON_ARRAYAGG

説明​

この関数は、結果を単一のJSON配列に集約します。配列の要素は各行で構成されます。この配列内の要素の順序は定義されていません。

この関数は、単一の値として評価される列または式に作用します。

構文​

JSON_ARRAYAGG(col_or_expr)[over_clause]

説明​

パラメータ col_or_expr は列または式です。結果に行が含まれない場合、またはエラーが発生した場合は、NULL を返します。

ヒント

この関数は、現在ウィンドウ関数、すなわち OVER 句をサポートしていません。

例​

CREATE TABLE tbl1 (oid INT, attr VARCHAR(100), value VARCHAR(100));
Query OK, 0 rows affected (0.000 sec)

INSERT INTO tbl1 VALUES (2, 'color', 'red'),(2, 'fabric', 'silk'),(3,'color','green'),(3,'shape','square');
Query OK, 4 rows affected (0.000 sec)
Records: 4 Duplicates: 0 Warnings: 0

SELECT oid, JSON_ARRAYAGG(attr) AS attributes FROM tbl1 GROUP BY oid;
+------+---------------------+
| oid | attributes |
+------+---------------------+
| 2 | ["color", "fabric"] |
| 3 | ["color", "shape"] |
+------+---------------------+
2 rows in set (0.001 sec)

INSERT INTO tbl1 SELECT * FROM tbl1;
Query OK, 4 rows affected (0.000 sec)
Records: 4 Duplicates: 0 Warnings: 0

SELECT oid, JSON_ARRAYAGG(attr) AS attributes FROM tbl1 GROUP BY oid;
+------+----------------------------------------+
| oid | attributes |
+------+----------------------------------------+
| 2 | ["color", "fabric", "color", "fabric"] |
| 3 | ["color", "shape", "color", "shape"] |
+------+----------------------------------------+
2 rows in set (0.001 sec)