为写得不好的标题道歉。
我有两张桌子。表a包含学生ID和姓名,表b包含学生ID,课程和课程费用
我希望能够查询数据库,以便显示学生姓名和课程花费的总金额
到目前为止,我有这个:
SELECT Student.fname, Student.lname, SUM(m.CourseCost) FROM
(SELECT DISTINCT StuID, CourseCost FROM Courses) AS m
INNER JOIN Student ON m.StuID = Student.ID;
当我尝试运行查询时,Visual Studio会引发错误。我使用MS Access作为数据库。我认为我的内心存在问题
答案 0 :(得分:0)
您的查询应该是
SELECT Student.fname, Student.lname, SUM(m.CourseCost) FROM
(SELECT DISTINCT StuID, CourseCost FROM Course) AS m
INNER JOIN Student ON m.StuID = Student.ID
group by Student.fname, Student.lname
或者你可以通过其他方式做到
select Student.fname, Student.lname, SUM(m.CourseCost) as CourseCost
from Student
inner join Course m on Student.ID = m.StuID
group by Student.fname, Student.lname, m.StuID
答案 1 :(得分:0)
select students.name , (select sum(cost) from course_details where student_id = students.id) as cost from students;
请注意,这是一个postgresql查询。我相信子查询的概念是一样的。
答案 2 :(得分:0)
尝试此查询
SELECT fname+' '+lname Name,courseName , SUM(CourseCost) CourseCost
FROM Student
INNER JOIN Courses ON Courses.StuID = Student.ID
GROUP BY fname, lname ,courseName ;