JSON_EXTRACT MariaDB

时间:2018-04-15 13:48:07

标签: sql mariadb

当我需要从json数据进行查询时,我遇到了问题。

我的json数据如下所示:

{"groups":[{"groupid":"3","group_name":"test group","group_type":"user","office":"1"},{"groupid":"4","group_name":"test group 1","group_type":"user","office":"1"},{"groupid":"21","group_name":"test group 2","group_type":"directory","office":"0"}]}

是否可以进行只返回office = 1

的groupids的查询

目前我已经这样做但不知道如何查询办公室。

select json_extract(test,'$.groups[*].groupid') as groupid from test;

1 个答案:

答案 0 :(得分:0)

尝试:

SELECT CONCAT('
  SELECT JSON_EXTRACT(`doc_text`, ',
    REPLACE(TRIM('[' FROM TRIM(']' FROM
        JSON_SEARCH(doc_text, 'all', '1', NULL, '$.groups[*].office')
        )
      ),
      'office',
      'groupid'
    ), ') `groupid` FROM `docs`'
) INTO @`sql`
FROM `docs`;

请参阅dbfiddle