最新下载
热门教程
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 
Mysql将查询结果集转换为JSON数据代码实例
时间:2022-06-29 08:44:31 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下Mysql将查询结果集转换为JSON数据代码实例,文章介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
Mysql将查询结果集转换为JSON数据 前言学生表学生成绩表查询单个学生各科成绩(转换为对象JSON串并用逗号拼接)将单个学生各科成绩转换为数组JSON串将数组串作为value并设置key两张表联合查询(最终SQL,每个学生各科成绩)最终结果
前言
我们经常会有这样一种需求,一对关联关系表,一对多的关系,使用一条sql语句查询两张表的所有记录,例:一张学生表,一张学生各科成绩表,我们想要用一条SQL查询出每个学生各科成绩;
学生表
CREATE TABLE IF NOT EXISTS `student`( `id` INT UNSIGNED AUTO_INCREMENT, `name` VARCHAR(100) NOT NULL PRIMARY KEY ( `id` ) )ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO student( id, name ) VALUES ( 1, '张三' ); INSERT INTO student( id, name ) VALUES ( 2, '李四' );
学生成绩表
CREATE TABLE IF NOT EXISTS `score`( `id` INT UNSIGNED AUTO_INCREMENT, `name` VARCHAR(100) NOT NULL `student_id` INT(100) NOT NULL, `score` VARCHAR(100) NOT NULL PRIMARY KEY ( `id` ) )ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO score( id, name, student_id, score) VALUES ( 1, '数学', 1, '95.5' ); INSERT INTO score( id, name, student_id, score) VALUES ( 2, '语文', 1, '99.5' ); INSERT INTO score( id, name, student_id, score) VALUES ( 3, '数学', 2, '95.5' ); INSERT INTO score( id, name, student_id, score) VALUES ( 4, '语文', 2, '88' );
查询单个学生各科成绩(转换为对象JSON串并用逗号拼接)
SELECT GROUP_CONCAT(JSON_OBJECT( 
'id',id,'name',name,'student_id',student_id, 'score', score)) as scores FROM scroe where student_id = 1;
## 查询结果
## {"id": 1, "name": "数学", "student_id": 1, "score": "95.5"},{"id": 2, "name": "语文", "student_id": 1, "score": "99.5"}
将单个学生各科成绩转换为数组JSON串
SELECT CONCAT('[', GROUP_CONCAT(JSON_OBJECT( 
'id',id,'name',name,'student_id',student_id, 'score', score)), ']') as scores FROM scroe where student_id = 1
## 查询结果
## [{"id": 1, "name": "数学", "student_id": 1, "score": "95.5"},{"id": 2, "name": "语文", "student_id": 1, "score": "99.5"}]
将数组串作为value并设置key
SELECT CONCAT('{"scoreData":[', GROUP_CONCAT(JSON_OBJECT( 
'id',id,'name',name,'student_id',student_id, 'score', score)), ']}') as scores FROM scroe where student_id = 1
## 查询结果
## {"scoreData": [{"id": 1, "name": "数学", "student_id": 1, "score": "95.5"},{"id": 2, "name": "语文", "student_id": 1, "score": "99.5"}]}
两张表联合查询(最终SQL,每个学生各科成绩)
SELECT id, name,
(SELECT CONCAT('[', GROUP_CONCAT(JSON_OBJECT( 
'id',id,'name',name,'student_id',student_id, 'score', score)), ']') as scores FROM scroe WHERE student_id = stu.id) AS scores
from student stu
## [{"id": 1, "name": "数学", "student_id": 1, "score": "95.5"},{"id": 2, "name": "语文", "student_id": 1, "score": "99.5"}]
最终结果
相关文章
- 过山车之星2游乐园声誉提升方法分享 11-04
 - 过山车之星2独特景点作用介绍说明 11-04
 - 炉石传说标准模式轮盘术卡组推荐分享 11-04
 - 炉石传说标准小行星萨卡组推荐分享 11-04
 - 炉石传说基尔加丹轮盘术卡组推荐分享 11-04
 - 炉石传说宇宙彩虹DK高胜率卡组推荐分享 11-04
 
            
                                
                                
                                
                                
                                            
                                        
                                            
                                        
                                            
                                        
                                            
                                        
                                            
                                        
                                            
                                        
                                            
                                        
                                            
                                        
                                            
                                        
                                            
                                        