JSON_ARRAYAGG() ist die Aggregatfunktion von MariaDB zum Aufbau von JSON-Arrays. Sie arbeitet auf einer Spalte oder einem Ausdruck, und jede Zeile der Ergebnismenge wird zu einem Element des erzeugten Arrays. Praktisch, wenn eine komplette Ergebnismenge als einzelner JSON-Wert geliefert werden soll.
Syntax
JSON_ARRAYAGG([DISTINCT] expr [,expr ...] [ORDER BY {unsigned_integer | col_name | expr} [ASC | DESC] [,col_name ...]] [LIMIT {[offset,] row_count | row_count OFFSET offset}])
Der Ausgangspunkt
SELECT PetName FROM Pets;
Ergebnis:
+---------+ | PetName | +---------+ | Fluffy | | Fetch | | Scratch | | Wag | | Tweet | | Fluffy | | Bark | | Meow | +---------+
Acht Zeilen, eine Spalte. Hüllt man die Spalte in JSON_ARRAYAGG(), werden aus diesen Zeilen Array-Elemente:
SELECT JSON_ARRAYAGG(PetName) FROM Pets;
Ergebnis:
+-------------------------------------------------------------------+ | JSON_ARRAYAGG(PetName) | +-------------------------------------------------------------------+ | ["Fluffy","Fetch","Scratch","Wag","Tweet","Fluffy","Bark","Meow"] | +-------------------------------------------------------------------+
Dubletten entfernen
DISTINCT eliminiert wiederholte Werte:
SELECT JSON_ARRAYAGG(DISTINCT PetName) FROM Pets;
Ergebnis:
+----------------------------------------------------------+ | JSON_ARRAYAGG(DISTINCT PetName) | +----------------------------------------------------------+ | ["Bark","Fetch","Fluffy","Meow","Scratch","Tweet","Wag"] | +----------------------------------------------------------+
Fluffy erscheint jetzt einmal statt zweimal, weil in der Tabelle zwei Tiere dieses Namens stehen.
Die Reihenfolge steuern
SELECT JSON_ARRAYAGG(PetName ORDER BY PetName DESC) FROM Pets;
Ergebnis:
+-------------------------------------------------------------------+ | JSON_ARRAYAGG(PetName ORDER BY PetName DESC) | +-------------------------------------------------------------------+ | ["Wag","Tweet","Scratch","Meow","Fluffy","Fluffy","Fetch","Bark"] | +-------------------------------------------------------------------+
Die Elementzahl begrenzen
SELECT JSON_ARRAYAGG(PetName LIMIT 3) FROM Pets;
Ergebnis:
+--------------------------------+ | JSON_ARRAYAGG(PetName LIMIT 3) | +--------------------------------+ | ["Fluffy","Fetch","Scratch"] | +--------------------------------+
Ein Offset überspringt Elemente, bevor die Zählung beginnt:
SELECT JSON_ARRAYAGG(PetName LIMIT 3 OFFSET 2) FROM Pets;
Ergebnis:
+-----------------------------------------+ | JSON_ARRAYAGG(PetName LIMIT 3 OFFSET 2) | +-----------------------------------------+ | ["Scratch","Wag","Tweet"] | +-----------------------------------------+
Man kann die Schlüsselwörter auch weglassen und die Zahlen vertauschen, getrennt durch ein Komma:
SELECT JSON_ARRAYAGG(PetName LIMIT 2, 3) FROM Pets;
Ergebnis:
+-----------------------------------+ | JSON_ARRAYAGG(PetName LIMIT 2, 3) | +-----------------------------------+ | ["Scratch","Wag","Tweet"] | +-----------------------------------+
Ein Array pro Gruppe
Nun kommt die Spalte PetTypeId dazu:
SELECT PetTypeId, PetName FROM Pets;
Ergebnis:
+-----------+---------+ | PetTypeId | PetName | +-----------+---------+ | 2 | Fluffy | | 3 | Fetch | | 2 | Scratch | | 3 | Wag | | 1 | Tweet | | 3 | Fluffy | | 3 | Bark | | 2 | Meow | +-----------+---------+
Gruppiert man nach dieser Spalte, erhält jede Tierart ein eigenes Array:
SELECT PetTypeId, JSON_ARRAYAGG(PetName) FROM Pets GROUP BY PetTypeId;
Ergebnis:
+-----------+---------------------------------+ | PetTypeId | JSON_ARRAYAGG(PetName) | +-----------+---------------------------------+ | 1 | ["Tweet"] | | 2 | ["Fluffy","Scratch","Meow"] | | 3 | ["Fetch","Wag","Fluffy","Bark"] | +-----------+---------------------------------+
IDs sagen einem Menschen, der die Ausgabe liest, wenig – also per JOIN auf PetTypes:
SELECT pt.PetType, p.PetName FROM Pets p INNER JOIN PetTypes pt ON pt.PetTypeId = p.PetTypeId ORDER BY PetType;
Ergebnis:
+---------+---------+ | PetType | PetName | +---------+---------+ | Bird | Tweet | | Cat | Scratch | | Cat | Fluffy | | Cat | Meow | | Dog | Wag | | Dog | Fetch | | Dog | Bark | | Dog | Fluffy | +---------+---------+
Und dann auf dem JOIN aggregieren:
SELECT pt.PetType, JSON_ARRAYAGG(p.PetName) FROM Pets p INNER JOIN PetTypes pt ON pt.PetTypeId = p.PetTypeId GROUP BY pt.PetType;
Ergebnis:
+---------+--------------------------+ | PetType | JSON_ARRAYAGG(p.PetName) | +---------+--------------------------+ | Bird | ["Tweet"] | | Cat | ["Scratch","Fluffy","Meow"] | | Dog | ["Wag","Fetch","Bark","Fluffy"] | +---------+--------------------------+