[英]SQL Query to Sum Values on One Table and Select Names from Another table
Apologies for the poorly written title. 不好意思的标题表示歉意。
I have two tables. 我有两张桌子。 Table a with Student ID and Names and table b with Student ID, course and course cost
具有学生证和姓名的表a和具有学生证,课程和课程费用的表b
I want to be able to query the database so I can show the student name and total dollars spent on courses 我希望能够查询数据库,以便显示学生姓名和课程总花费
So far I have this: 到目前为止,我有这个:
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 throws an error when I try to run the query. 当我尝试运行查询时,Visual Studio引发错误。 I am using MS Access as the database.
我正在使用MS Access作为数据库。 I think there is something wrong with my inner
我认为我的内心有问题
Your query should be 您的查询应为
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
Or You can do by another way 或者你可以用另一种方式
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;
please note, this is a postgresql query. 请注意,这是一个PostgreSQL查询。 I believe the concept of subquery is the same.
我相信子查询的概念是相同的。
Try this query 试试这个查询
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.