mysql8.0 提取json数据转为行

仅适用于mysql8.0,之前的版本不支持JSON_TABLE函数

数据结构:

id name jsonarr
1 张三 [{"daystr": "2023-12-01","score": "53"},{"daystr": "2023-12-02","score": "78"}]
2 李四 [{"daystr": "2023-12-01","score": "65"},{"daystr": "2023-12-02","score": "68"}]

SQL:

SELECT
	id,name,t.daystr as '日期',t.v as '成绩数据'
FROM
	tableName as temp,
	JSON_TABLE(temp.jsonarr,'$[*]' COLUMNS (daystr varchar(20) PATH '$.daystr ',v json PATH '$')) t 
	

查询结果:

id name 日期 成绩数据
1 张三 2023-12-01 {"daystr": "2023-12-01","score": "53"}
1 张三 2023-12-02 {"daystr": "2023-12-02","score": "78"}
2 李四 2023-12-01 {"daystr": "2023-12-01","score": "65"}
2 李四 2023-12-02 {"daystr": "2023-12-02","score": "68"}

你可能感兴趣的:(数据库,数据库,mysql)