MySQL:打开和转置列表指南
P粉103739566
P粉103739566 2023-09-07 13:24:07
0
2
409

我有一个给定的数据框:

id 数字列表
2 [1,2,5,6,7]
5 [1,2,13,51,12]

其中一列只是 id,另一列是数字列表,就像我之前从 JSON 文件中获得的那样,有没有办法仅使用 MySQL 将其转换为这种格式?

id 数字列表
2 1
2 2
2 5
2 6
2 7
5 1
5 2
5 13
5 51
5 12

我知道使用Python和pandas可以轻松完成,但在这种情况下我只需要使用MySQL,而且我真的不知道如何在MySQL中转置列表

P粉103739566
P粉103739566

全部回复(2)
P粉786432579

@Ruslan Pylypiuk

Postgresql 灵魂:

select 
id,regexp_split_to_table(listofnumbers,',')
from test

Mysql灵魂:参考SQL将值拆分为多行

P粉883223328

您可以使用json_table()

create table myTable(id int, listofnumbers varchar(200));
insert into myTable values(2,   '[1, 2, 5, 6, 7]');
insert into myTable values(5,   '[1, 2, 13, 51, 12]');
select t.id, j.listofnumbers
from myTable t
join json_table(
  t.listofnumbers,
  '$[*]' columns (listofnumbers varchar(50) path '$')
) j;
id 数字列表
2 1
2 2
2 5
2 6
2 7
5 1
5 2
5 13
5 51
5 12
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!