更新查询以根据另一个表中的值更改一个表中列的现有值

时间:2017-08-05 22:03:26

标签: sql ms-access

我正在使用微软访问,我总共有3个表:Credits,Orders和Books,如下所示。我需要创建一个更新查询,根据他们订购的书籍以及每本书的相应学分数来更新每个学生的现有学分数。

例如,学生B-17以24学分开始,但在更新查询后,应将学生的学分改为32学分。

学分表

Student ID    Number of Credits
B-17          24
F-59          30

订单表

Student ID    Book ID
B-17          101
B-17          102
F-59          101
F-59          105

书籍表

Book ID    Book Title    Credits
101        English I     3
102        Accounting    5
105        Calculus      5

这就是我正在尝试但我在Access中遇到语法错误。

UPDATE Credits c
SET [Number of Credits] = [Number of Credits] + (SELECT SUM(Credits)
FROM Orders o, Books b ON
o.[Book ID] = b.[Book ID] WHERE 
c.[Student ID] = o.[Student ID])
WHERE c.[Student ID] = o.[Student ID];

1 个答案:

答案 0 :(得分:0)

您可以尝试使用TEMP表存储来自第2和第3个表的数据;这样做:1)创建另一个第四个表(即Temp),其中包含两列:学生ID和学分; 2)运行此脚本:

SELECT o.[Student ID], sum (b.Credits) as Credits INTO Temp 
FROM books b INNNER JOIN orders o
on b.[Book ID] = o.[Book ID] 
GROUP BY o.[Student ID];

3)开始更新:

UPDATE Credits c, Temp t set c.[Number of Credits] = c.[Number of Credits] + t.Credits
WHERE c.[Student ID] = t.[Student ID];

4)保存并完成!

有用:1)“Operation must use an updateable query" error in MS Access;         2)MS Access database (2010) how to create temporary table/procedure/view from Query Designer