How to get unique / distinct elements inside JSON array in MySQL 5.7

扶醉桌前 提交于 2019-12-12 14:52:38

问题


I have mysql 5.7 with a denormalized table that has some JSON columns. I need to extract unique / distinct values per row for an array column.

For e.g: ["a", "b", "b", "a", "c"] expected output should be ["a", "b", "c"];

SET @json = '["a", "b", "b", "a", "c"]'; I need to get unique values in this list.

["a", "b", "c"];


回答1:


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



来源:https://stackoverflow.com/questions/58051995/how-to-get-unique-distinct-elements-inside-json-array-in-mysql-5-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!