最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mysql - Group by a part of json array column - Stack Overflow

programmeradmin3浏览0评论

I have a table with path column that is json.

The values are something like:

[1]
[1,2,3]
[1,5,10]

I would like to GROUP BY the first value (in this case 1).

I have a table with path column that is json.

The values are something like:

[1]
[1,2,3]
[1,5,10]

I would like to GROUP BY the first value (in this case 1).

Share Improve this question asked Feb 14 at 17:11 Giacomo MGiacomo M 4,7237 gold badges30 silver badges68 bronze badges 2
  • GROUP BY path->"*[0]"? – Barmar Commented Feb 14 at 18:31
  • Do you think extracting from a JSON array is different in GROUP BY? – Barmar Commented Feb 14 at 18:33
Add a comment  | 

1 Answer 1

Reset to default 1
CREATE TABLE test (val JSON)
SELECT '[1]' val UNION ALL
SELECT '[1,2,3]' UNION ALL
SELECT '[1,5,10]'UNION ALL
SELECT '[2,2,3]' UNION ALL
SELECT '[2,5,10]'UNION ALL
SELECT '[123,5,10]';
SELECT CAST(val AS CHAR) FROM test;
SELECT val->"$[0]" + 0, COUNT(*) FROM test GROUP BY 1; 
CAST(val AS CHAR)
[1]
[1, 2, 3]
[1, 5, 10]
[2, 2, 3]
[2, 5, 10]
[123, 5, 10]
val->"$[0]" + 0 COUNT(*)
1 3
2 2
123 1

fiddle

发布评论

评论列表(0)

  1. 暂无评论