[英]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.