![](/img/trans.png)
[英]ORACLE SQL - SELECT SUM from One Table With Conditions in Another Table
[英]SQL Query to Sum Values on One Table and Select Names from Another table
不好意思的标题表示歉意。
我有两张桌子。 具有学生证和姓名的表a和具有学生证,课程和课程费用的表b
我希望能够查询数据库,以便显示学生姓名和课程总花费
到目前为止,我有这个:
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作为数据库。 我认为我的内心有问题
您的查询应为
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
select students.name , (select sum(cost) from course_details where student_id = students.id) as cost from students;
请注意,这是一个PostgreSQL查询。 我相信子查询的概念是相同的。
试试这个查询
SELECT fname+' '+lname Name,courseName , SUM(CourseCost) CourseCost
FROM Student
INNER JOIN Courses ON Courses.StuID = Student.ID
GROUP BY fname, lname ,courseName ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.