将所有json列转换为新表

y4ekin9u  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(552)

我目前有一个表的结构如下:

  1. customer_id name phoneNumbers
  2. 1 Adam [{'type':'home','number':'687-5309'} , {'type':'cell','number':'123-4567'}]
  3. 2 Bill [{'type':'home','number':'987-6543'}]

将phonenumbers列设置为json列类型。为了简单起见,我想把所有的json电话号码转换成一个新的单独的表。比如:

  1. phone_id customer_id type number
  2. 1 1 home 687-5309
  3. 2 1 cell 123-4567
  4. 3 2 home 987-6543

似乎它应该可以用openjson实现,但到目前为止,我还没有找到正确声明它的方法。感谢您的帮助。

ugmeyewa

ugmeyewa1#

你可以这样做:

  1. SELECT id,
  2. name,
  3. JSON_UNQUOTE(JSON_EXTRACT(phone, CONCAT("$[", seq.i, "]", ".", "number"))) AS NUMBER,
  4. JSON_UNQUOTE(JSON_EXTRACT(phone, CONCAT("$[", seq.i, "]", ".", "type"))) AS TYPE
  5. FROM customer, (SELECT 0 AS I UNION ALL SELECT 1) AS seq
  6. WHERE seq.i < json_length(phone)

诀窍是 (SELECT 0 as i union all SELECT 1) ,取决于json数组的长度,您可能需要添加更多索引。您可以通过以下方法找到最大长度:

  1. SELECT MAX(JSON_LENGTH(phone)) FROM customer;
lsmd5eda

lsmd5eda2#

使用带1的递归cte,并递归到json\u长度。

  1. SELECT c.*, JSON_LENGTH(c.phoneNumbers) as json_length
  2. from customers c;

然后使用concat在extract查询中传递该元素的\u id:

  1. (json_unquote(JSON_EXTRACT(phoneNumbers, CONCAT('$.type.',1))), json_unquote(JSON_EXTRACT(phoneNumbers, CONCAT('$.number.',1))))
  2. (json_unquote(JSON_EXTRACT(phoneNumbers, CONCAT('$.type.',2))), json_unquote(JSON_EXTRACT(phoneNumbers, CONCAT('$.number.',1))))
  3. -
  4. -
  5. -
  6. (json_unquote(JSON_EXTRACT(phoneNumbers, CONCAT('$.type.',json_length))), json_unquote(JSON_EXTRACT(phoneNumbers, CONCAT('$.number.',json_length))))
a11xaf1n

a11xaf1n3#

请根据mysql\maria版本更改cte定义语法。

  1. WITH RECURSIVE cte_recurse_json AS
  2. (
  3. SELECT customer_id, phone_numbers, 0 as recurse, JSON_LENGTH(c.phoneNumbers) as json_length,
  4. json_unquote(JSON_EXTRACT(phoneNumbers, CONCAT('$[',0,'].type'))) as type,
  5. json_unquote(JSON_EXTRACT(phoneNumbers, CONCAT('$[',0,'].number'))) as number
  6. FROM table
  7. UNION ALL
  8. SELECT t.customer_id, t.phone_numbers, ct.recurse + 1 as recurse, t.json_length,
  9. json_unquote(JSON_EXTRACT(ct.phoneNumbers, CONCAT('$[',ct.recurse,'].type'))) as type,
  10. json_unquote(JSON_EXTRACT(ct.phoneNumbers, CONCAT('$[',ct.recurse,'].number'))) as number
  11. FROM TABLE t
  12. INNER JOIN cte_recurse_json ct ON t.customer_id = ct.customer_id
  13. WHERE ct.recurse < json_length
  14. )
  15. SELECT customer_id, type, number FROM cte_recurse_json;

相关问题