mysql 一对多联合group查询, 将多条记录合并成一条记录, mysql两表联合查询,一对多

 

 

方式1:

复制代码
select cjc.uid,u.uname,cjc.数学,cjc.英语,cjc.语文,u.type from 
 (select uid,max(case subj when '数学' then score else 0 end) 数学,
 max(case subj when '语文' then score else 0 end) 语文,
 max(case subj when '英语' then score else 0 end) 英语 
 from cj
 group by uid) cjc
 left join
 t_user u
 on u.uid=cjc.uid
复制代码

方式2:

复制代码
select cjc.uid,u.uname,cjc.数学,cjc.英语,cjc.语文,u.type from 
 (select uid,max(case subj when '数学' then score else 0 end) 数学,
 max(case subj when '语文' then score else '' end) 语文,
 max(case subj when '英语' then score else '' end) 英语 
 from cj
 group by uid) cjc
 left join
 t_user u
 on u.uid=cjc.uid
复制代码

方式3:

SELECT a.uid,a.uname,a.type,GROUP_CONCAT(b.subj,b.score) from t_user a LEFT JOIN cj b ON a.uid=b.uid GROUP BY a.uid

方式4:

select u.uid,u.uname,cjc.数学,cjc.英语,cjc.语文,u.type from t_user u left join
 (select uid,max(case subj when '数学' then score else '' end) 数学,
 max(case subj when '语文' then score else '' end) 语文,
 max(case subj when '英语' then score else '' end) 英语 from cj group by uid) cjc
 on u.uid=cjc.uid

 

posted @   study_php_java_C++  阅读(240)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示