返回学分数的mysql查询

时间:2014-03-26 07:53:25

标签: mysql

有三个表(学生,年级,课程)

table1: students 
Name  ,  Id
Joseph,  1
David ,  2

table2: grade
Id(fk of students.id) , Course(fk of course.courseName),Grade
1 , math101 , A
1 , math103 , B
1 , eng201 , A
2 , eng201 , B
2 , math103, B

table3: course
courseName , credits
math101 , 3
math103 , 4
eng201  , 3

我希望每个名字都有他/她赚取的总积分数

QUERY...
result -->

ID,NAME,TotalCredit
1,Joseph,10
2,David,7

1 个答案:

答案 0 :(得分:1)

试试这个:

   SELECT s.id, s.name, sum(c.credits)
     FROM students as s
LEFT JOIN grade as g on s.id = g.id
LEFT JOIN course as c on g.course = c.courseName
 GROUP BY s.id, s.name