在 SQL 中如何在更新中使用 Sum 函数 [英] In SQL How is it possible to use the Sum Function in an Update

查看:99
本文介绍了在 SQL 中如何在更新中使用 Sum 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用的代码

UPDATE Customers c   
SET c.TotalP= 
   (SELECT SUM(b.Price) 
    FROM Products b
   WHERE b.CustomerID= c.CustomerID)

我正在尝试将值 x 设置为客户表中的 TotalP 字段.值 x 等于产品表中客户 ID 相同的核心响应价格的总和.

I im trying to set a value x to the TotalP field in the Customers table. The value x is equal to the sum of the coressponding prices in the Products table where the Customer ID's are the same.

上面的代码返回一个错误操作必须是一个可更新的查询"

The above code returns an error of "Operation must be an Updateable query"

推荐答案

您可以使用 DSum:

You can use DSum:

UPDATE Customers   
SET TotalP = DSum("Price","Products","CustomerID = " & CustomerID)

但它确实提出了一个问题,即当可以从查询中获取信息时,为什么要使用计算值更新表.

But it does raise the question as to why you are updating a table with a calculated value when the information can be obtained from a query.

这篇关于在 SQL 中如何在更新中使用 Sum 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆