我的数据库中有两个表。第一个表包含列Serial,Date,Location,Type,Condition,Color,Loaned_To,ProductNumberREF和Department。
下表包含产品编号,类型,品牌和价格。
使用这个系统,我可以不改变主表中所有项目的价格。但现在我遇到了一个问题。
我正在创建一个总金额支出标签,我希望填充Gridview中所有项目的所有价格总和。如何在不包含价格但必须引用另一个表格的表格中添加所有价格的总和?
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
conn.Open();
SqlCommand SpentCheck = new SqlCommand("Selct Sum(Price) From Items");
此代码是我最初的尝试,直到我意识到PRICE在ITEMS中不存在。那么我如何得到ITEMS中所有价格的总和呢?
更新#####
我尝试了第一个答案建议这是我现在
答案 0 :(得分:2)
您正在寻找SQL join。
SELECT
SUM(Price)
FROM
Items
INNER JOIN ProductNumber ON Items.Product_NumberREF = ProductNumber.ProductNumber
答案 1 :(得分:-1)
尝试
SELECT SUM(P.Price) AS total FROM ProductNumber P, Item I WHERE I.Product_NumberREF = P.ProductNumber
例如,如果您可以按Serial过滤,请执行:
SELECT SUM(P.Price) AS total FROM ProductNumber P, Item I WHERE I.Product_NumberREF = P.ProductNumber AND Serial = @Serial
并像这样实例化sqlcommand:
SqlCommand SpentCheck =新的SqlCommand(YOUR_SQL,conn);