There is no direct method to get distinct values out of a JSON array in MySQL. One method could be to utilize a Sequence/Number Generator table concept. This sequence table could be used as a Derived Table (subquery), or you can create a permanent table storing numbers in your database.
We will then use this sequence table to JSON_EXTRACT()
values out from array at first key, second key, third key and so on. Once we have extracted out the values in separate row, we can simply use DISTINCT
to get unique values out of them. Afterwards, we can use JSON_ARRAYAGG()
function to re-aggregate these unique values back as a JSON array.
Schema (MySQL v5.7)
SET @json = '["a", "b", "b", "a", "c"]';
Query
SELECT Json_arrayagg(dt.val) AS unq_json_array
FROM (SELECT DISTINCT Json_extract(@json, Concat('$[', seq.n, ']')) AS val
FROM (SELECT 0 AS n UNION ALL SELECT 1 UNION ALL
SELECT 2 UNION ALL SELECT 3 UNION ALL
SELECT 4 UNION ALL SELECT 5 UNION ALL
SELECT 6 UNION ALL SELECT 7 UNION ALL
SELECT 8 UNION ALL SELECT 9) AS seq) AS dt
WHERE dt.val IS NOT NULL;
Result
| unq_json_array |
| --------------- |
| ["a", "b", "c"] |
View on DB Fiddle
DISTINCT
on the values, then store each value in a separate row. In other words, use JSON only to store and fetch the whole JSON document verbatim. Do not expect to use SQL operations to search, sort, or aggregate elements of JSON documents as if they were discrete relational data.