如何选择这样的2张桌子

编程入门 行业动态 更新时间:2024-10-22 15:43:07
本文介绍了如何选择这样的2张桌子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何选择具有这种条件的表.

How to select table with condition like this.

我有一张桌子,上面放着各种各样的主题.

I have a table that holds all kinds of subjects like this.

table subjects |===========|============| |Subjects id| Subjects | |===========|============| | 01 |mathematics | | 02 |biology | | 03 |geography | | 04 |physics | |===========|============|

然后我还有一个表格来保存每个这样的学生的价值.

then I also have a table to hold the value of each student like this.

table score |==========|============|===============| |Student id|Subjects id | Score | |==========|============|===============| | 10001 | 01 | 8 | | 10001 | 02 | 6 | | 10001 | 03 | 7 | | 10001 | 04 | 9 | | 10002 | 01 | 5 | | 10002 | 02 | 7 | | 10002 | 03 | 10 | | 10002 | 04 | 7 | | 10003 | 01 | 6 | | 10003 | 02 | 7 | | 10003 | 03 | 8 | | 10003 | 04 | 9 | |==========|============|===============|

我想用下表形式创建查询,但我不知道该怎么做.

I want to create a query with a form like the following table but i dont know how to make it.

|==========|=============|=========|===========|=========| |Student id| mathematics | biology | geography | physics | |==========|=============|=========|===========|=========| | 10001 | 8 | 6 | 7 | 9 | | 10002 | 5 | 7 | 10 | 7 | | 10003 | 6 | 7 | 8 | 9 | |==========|=============|=========|===========|=========|

请帮助我解决这个问题.对不起,我的英语不好.我仍然是初学者

please help me to solve this problem. Sorry my english is bad. I am still beginner

推荐答案

您有一些方法可以做到这一点,但是如果不创建临时表,可以执行以下操作:

You have some ways to do that, but trying not to create temporary tables, you can do something like that:

select s.id, avg(case when sb.id = '01' then s.score end) as math, avg(case when sb.id = '02' then s.score end) as bio from student s join subject sb on (sb.id = s.subject_id) group by s.id

只需根据需要将总和/格行填入其他主题!

Just fill the sum/case lines to the other subjects as you need!

希望有帮助.

更多推荐

如何选择这样的2张桌子

本文发布于:2023-10-28 18:15:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537392.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何选择   桌子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!