简体   繁体   English

将一个SQL查询的结果合并到一个结果中

[英]Combine the results of a sql query together in one result

I have an application(C#) that manages a stock level for every storage unit. 我有一个应用程序(C#),它管理每个存储单元的库存水平。 But now i want a complete overview of the stock level of every product from every stock unit together. 但是,现在我希望对每个库存单位的每种产品的库存水平有一个完整的了解。

For example i have 50 apples (ProductID 1) in unit one and 25 in unit two and I use this query: 例如,我在第一单元中有50苹果(产品ID 1),在第二单元中有25 50苹果,我使用以下查询:

 select StockLevel 
   from Supply 
  where ProductID = '1' 

And put the result in a textbox it will always gives me the first stocklevel of 50 apples and not 75 . 并将结果放在一个文本框中,它总是给我第一个库存的苹果是50个苹果,而不是75个

Is there a way i can combine these results in an easy way? 有没有一种方法可以轻松组合这些结果?

If you want to combine , you want an aggregate function , sum in your case: 如果要合并 ,则需要聚合函数sum为:

 select sum(StockLevel)
   from Supply
  where ProductID = '1' 

and get the apples being summed up: 75 which is 50 + 25 得到苹果的总和: 75等于50 + 25

你可以这样尝试

SELECT (select sum(stock) from tablename where ProductID = 1) + (select sum(stock) from tablename where ProductID  =1) as result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM