简体   繁体   English

MySQL 5.7如何将json数组提取到多列?

[英]mysql 5.7 how to extract json array to multiple columns?

I'm trying to figure out how to extract a json array to individual columns. 我试图弄清楚如何将json数组提取到各个列。 Working in mysql 5.7. 在mysql 5.7中工作。

the data i'd like to extract is held in the column 'data'. 我要提取的数据保存在“数据”列中。 here is sample of data: 这是数据示例:

# id, member_id, event_type_id, leeds_event_id, source_event_id, staff_id, staff_name, date_start, date_end, data, created, deleted, id, source, name, created, deleted
6852184, 0034000000y3RSxAAM, 1, 1, , , Peter Nalli, , 2013-08-07 00:00:00, {"id": 1, "completed": 0, "member_id": "0034000000y3RSxAAM", "assigned_staff": "Peter Nalli", "date_completed": "2013-08-07"}, 2018-11-26 18:43:45, , 1, leeds_new.leenk_1o1s, 1 on 1s, 2018-07-24 11:14:27, 
6852185, 0034000001rmsjRAAQ, 1, 2, , , Marcus Ceniceros, , 2016-11-22 00:00:00, {"id": 2, "completed": 0, "member_id": "0034000001rmsjRAAQ", "assigned_staff": "Marcus Ceniceros", "date_completed": "2016-11-22"}, 2018-11-26 18:43:45, , 1, leeds_new.leenk_1o1s, 1 on 1s, 2018-07-24 11:14:27, 
6852186, 00340000010SqytAAC, 1, 3, , , Stephen de Man, , 2013-06-26 00:00:00, {"id": 3, "completed": 0, "member_id": "00340000010SqytAAC", "assigned_staff": "Stephen de Man", "date_completed": "2013-06-26"}, 2018-11-26 18:43:45, , 1, leeds_new.leenk_1o1s, 1 on 1s, 2018-07-24 11:14:27, 
6852187, 00340000013znbgAAA, 1, 4, , , Peter Nalli, , 2013-05-23 00:00:00, {"id": 4, "completed": 0, "member_id": "00340000013znbgAAA", "assigned_staff": "Peter Nalli", "date_completed": "2013-05-23"}, 2018-11-26 18:43:45, , 1, leeds_new.leenk_1o1s, 1 on 1s, 2018-07-24 11:14:27, 
6852188, 0034000000y3RgPAAU, 1, 5, , , Stephen de Man, , 2013-05-15 00:00:00, {"id": 5, "completed": 0, "member_id": "0034000000y3RgPAAU", "assigned_staff": "Stephen de Man", "date_completed": "2013-05-15"}, 2018-11-26 18:43:45, , 1, leeds_new.leenk_1o1s, 1 on 1s, 2018-07-24 11:14:27, 
6852189, 0034000000y3SIRAA2, 1, 6, , , Stephen de Man, , 2013-03-06 00:00:00, {"id": 6, "completed": 0, "member_id": "0034000000y3SIRAA2", "assigned_staff": "Stephen de Man", "date_completed": "2013-03-06"}, 2018-11-26 18:43:45, , 1, leeds_new.leenk_1o1s, 1 on 1s, 2018-07-24 11:14:27, 
6852190, 0034000000y3Q3DAAU, 1, 7, , , Lildella Douglas, , 2017-01-02 00:00:00, {"id": 7, "completed": 0, "member_id": "0034000000y3Q3DAAU", "assigned_staff": "Lildella Douglas", "date_completed": "2017-01-02"}, 2018-11-26 18:43:45, , 1, leeds_new.leenk_1o1s, 1 on 1s, 2018-07-24 11:14:27, 
6852191, 0034000001Cr8MjAAJ, 1, 8, , , Thomas Beer, , 2015-06-26 00:00:00, {"id": 8, "completed": 0, "member_id": "0034000001Cr8MjAAJ", "assigned_staff": "Thomas Beer", "date_completed": "2015-06-26"}, 2018-11-26 18:43:45, , 1, leeds_new.leenk_1o1s, 1 on 1s, 2018-07-24 11:14:27, 
6852192, 0034000000y3S98AAE, 1, 9, , , Peter Nalli, , 2015-03-18 00:00:00, {"id": 9, "completed": 0, "member_id": "0034000000y3S98AAE", "assigned_staff": "Peter Nalli", "date_completed": "2015-03-18"}, 2018-11-26 18:43:45, , 1, leeds_new.leenk_1o1s, 1 on 1s, 2018-07-24 11:14:27,

As an example, I'd like to take the data: 例如,我想获取数据:

'{"id": 8, "completed": 0, "member_id": "0034000001Cr8MjAAJ", "assigned_staff": "Thomas Beer", "date_completed": "2015-06-26"}'

and extract to 5 separate new columns. 并提取到5个单独的新列。

I've been trying to sort this out with json_extract() but am not getting anywhere. 我一直在尝试用json_extract()来解决这个问题,但是没有任何进展。 I'm new to this and am getting a bit lost. 我是新来的人,迷路了。

Any help would be greatly appreciated. 任何帮助将不胜感激。

In SQL, each column in the select-list must be explicit at the time you prepare the query. 在SQL中,在准备查询时,选择列表中的每一列都必须是显式的。 There's no way to make a query that expands the select-list at runtime, depending on the data it finds. 无法进行查询以在运行时扩展选择列表,这取决于找到的数据。

So you need to extract each field from your JSON document separately. 因此,您需要分别从JSON文档中提取每个字段。

SELECT
  data->>'$.id' AS id,
  data->>'$.completed' AS completed,
  data->>'$.member_id' AS member_id,
  data->>'$.assigned_staff' AS assigned_staff,
  data->>'$.date_completed' AS data_completed
FROM MyTable

data->>'$.id' is equivalent to JSON_UNQUOTE(JSON_EXTRACT(data, '$.id')) . data->>'$.id'等效于JSON_UNQUOTE(JSON_EXTRACT(data, '$.id'))

I tested your data using MySQL 5.7.24, and this is the output of my query: 我使用MySQL 5.7.24测试了您的数据,这是查询的输出:

+------+-----------+--------------------+----------------+----------------+
| id   | completed | member_id          | assigned_staff | data_completed |
+------+-----------+--------------------+----------------+----------------+
| 8    | 0         | 0034000001Cr8MjAAJ | Thomas Beer    | 2015-06-26     |
+------+-----------+--------------------+----------------+----------------+

Honestly, in most cases, using JSON in MySQL would be better implemented as conventional tables and columns. 老实说,在大多数情况下,在MySQL中使用JSON可以更好地实现为常规表和列。 When your data is only a simple set of key/value pairs, it's not worth using JSON. 当您的数据只是一组简单的键/值对时,不值得使用JSON。


Re comments about generated columns: 关于生成的列的评论:

ALTER TABLE MyTable 
 ADD COLUMN completed INT AS (data->>'$.completed'),
 ADD COLUMN member_id VARCHAR(20) AS (data->>'$.member_id');

And so on. 等等。 You need to add a generated column for each field you want to extract from the JSON. 您需要为要从JSON中提取的每个字段添加一个生成的列。

Then you can query as if the fields were normal columns: 然后,您可以查询字段好像是普通列:

SELECT completed, member_id FROM MyTable;

+-----------+--------------------+
| completed | member_id          |
+-----------+--------------------+
|         0 | 0034000001Cr8MjAAJ |
+-----------+--------------------+

But this is not "automated" in any way. 但这绝不是“自动化”的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM