獲取數據I具有下表佈局DB2查詢而從3表
表一個
Teacher Students
Mohan pankaj
Mohan sudeepa
Mohan sujitra
Mohan Sumit
表B(學期1馬克)
Pankaj Maths 50
Pankaj English 40
Pankaj Physics 60
Sudeepa Chemisty 40
Sujitra Physic 10
Sujitra English 40
Sujitra Hindi 70
表c(學期2分)
Pankaj Chemistry 50
Pankaj English 40
Pankaj Physics 60
Sudeepa Science 40
Sujitra Physic 10
Sujitra Maths 40
Sujitra Hindi 70
我想作爲輸出: -
hindi 140.00
maths 40.00
physics 10.00
science 40.00
chemistry 40.00
english 40.00
physics 10.00
我寫的查詢
select subject,sum(marks) from sample.b where
student in (select student from sample.a where teacher='mohan')
group by subject
union all
select subject,sum(marks) from sample.c
where student in (select student from sample.test where teacher='mohan')
group by subject
我得到的結果作爲
hindi 70.00
maths 40.00
physics 10.00
science 40.00
chemistry 40.00
english 40.00
hindi 70.00
physics 10.00
* 發行 *Same subjects marks are not adding up
..
請注意,您的數據庫設計存在問題 - 您是否計劃爲_every_不同學期添加一張表?你最好擁有一張「Class_Taken」表(或類似的表),並在學年和學期增加一些額外的專欄。 –