我为我的标题含糊而道歉,因为我总是很难描述在SQL方面需要做些什么。我正在使用微软访问,我总共有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];
答案 0 :(得分:0)
您的逗号为ON
。你可以尝试:
UPDATE Credits as c INNER JOIN
(SELECT o.[Student ID], SUM(b.Credits) as Credits
FROM Orders as o INNER JOIN
Books as b
ON o.[Book ID] = b.[Book ID]
GROUP BY o.[Student ID]
) ob
ON c.[Student ID] = o.[Student ID]
SET c.[Number of Credits] = c.[Number of Credits] + ob.Credits;
我似乎常常无法使用连接语法来使用MS Access。代替:
UPDATE Credits
SET [Number of Credits] = [Number of Credits] +
(SELECT SUM(b.Credits) as Credits
FROM Orders as o INNER JOIN
Books as b
ON o.[Book ID] = b.[Book ID]
WHERE o.[Student ID] = Credits.[Student ID]
);
答案 1 :(得分:0)
您可以尝试使用TEMP
表来存储第2和第3个表中的数据;
这样做:
1)创建另一个包含两列的第4个表(即Temp):Student ID
和Credits
;
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