Car*_*sco 5 mysql arrays amazon-aurora
我知道我可以使用 MySQL 5.7.22 版本的 JSON_ARRAYAGG 函数来获得分组对象数组,但我在工作中使用旧版本。有人知道如何为 AroraDB 5.7.12 使用类似 JSON_ARRAYAGG 的函数?因为我需要将值分组到对象数组中。例如,我使用了 GROUP_CONCAT(),但结果是一串用逗号“,”分隔的对象字符串,我需要该结果是一个列表。类似的事情,例如:
This is my table:
+-----------------+---------------+
| State | City |
+-----------------+---------------+
| Capital Region | Canberra |
| New South Wales | Sydney |
| New South Wales | Newcastle |
| New South Wales | Central Coast |
| New South Wales | Wollongong |
| Queensland | Brisbane |
| Queensland | Gold Coast |
| Queensland | Townsville |
| Queensland | Cairns |
| South Australia | Adelaide |
| Tasmania | Hobart |
| Victoria | Melbourne |
| Victoria | Geelong |
| West Australia | Perth |
+-----------------+---------------+
And I want the next result:
+-----------------+--------------------------------------------------------+
| State | Cities |
+-----------------+--------------------------------------------------------+
| Capital Region | ["Canberra"] |
| New South Wales | ["Sydney", "Newcastle", "Central Coast", "Wollongong"] |
| Queensland | ["Brisbane", "Gold Coast", "Townsville", "Cairns"] |
| South Australia | ["Adelaide"] |
| Tasmania | ["Hobart"] |
| Victoria | ["Melbourne", "Geelong"] |
| West Australia | ["Perth"] |
+-----------------+--------------------------------------------------------+
So, is there a MySQL function to this?
Thanks for your attention!
Run Code Online (Sandbox Code Playgroud)
结合:GROUP_CONCATCONCAT
SELECT CAST(CONCAT('[',GROUP_CONCAT(CONCAT('"',city,'"')),']') AS JSON) AS cities
FROM tab
GROUP BY State;
Run Code Online (Sandbox Code Playgroud)