【问题标题】:Combine the results of a sql query together in one result将 sql 查询的结果合并为一个结果
【发布时间】:2016-10-26 09:16:11
【问题描述】:

我有一个应用程序 (C#) 来管理每个存储单元的库存水平。 但现在我想全面了解每个库存单位中每种产品的库存水平。

例如,我在第一单元中有50 apples (ProductID 1),在第二单元中有25,我使用以下查询:

 select StockLevel 
   from Supply 
  where ProductID = '1' 

并将结果放在文本框中,它总是会给我第一个库存级别的 50 个苹果,而不是 75

有没有一种方法可以简单地组合这些结果?

【问题讨论】:

  • Union 是你的朋友。
  • @MohitShrivastava 正要这么说
  • 你可以使用union(不要重复)否则使用union all
  • @LorenceHernandez 是同一件事的朋友。
  • 呃,sum? select sum(StockLevel) from Supply where ProductID = '1' 并得到 125 (= 50 +75)

标签: c# sql add


【解决方案1】:

如果你想合并,你需要一个聚合函数,在你的情况下sum

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

然后总结苹果:75,即50 + 25

【讨论】:

    【解决方案2】:

    你可以这样试试

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      相关资源
      最近更新 更多