UserID | Access |
---|---|
1 | ["1"] |
2 | ["2", "3"] |
3 | ["3"] |
I have the above table, I’m looking to return a merged array of all the Access values. So the result would be
["1","2","3"]
I have tried with:
SELECT JSON_ARRAYAGG(Access) FROM table;
But this results in a concatenated JSON array of arrays like:
[["1"],["2","3"],["3"]]
What is the best way of achieving the desired output?
2
Answers
TABLE
Query
output
I am going to answer this showing a PHP variant per my comment .. This is just meant to be helpful and possibly useful as an alternative to a complicated SQL call.
Say you had this table:
You can use this little bit of PHP to give you exactly what you’re looking for using 7 lines of code (provided you make your SQL call).
That will give you a JSON string that looks like:
Every language is different, but rest assured they all have the basic logic I have laid out here ..