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
4条答案
按热度按时间iyfamqjs1#
You have some ways to do that, but trying not to create temporary tables, you can do something like that:
Just fill the sum/case lines to the other subjects as you need!
Hope it helps.
62o28rlo2#
This should work, assuming there is no duplicate in the score table, and those are the only 4 subjects you have. Or you can use PIVOT too.
r6l8ljro3#
You can use a group by like the other answers or left joins like this:
oipij1gg4#
You can use: