【问题标题】:Sum of one column in special condition [closed]特殊条件下一列的总和[关闭]
【发布时间】:2015-04-09 15:28:46
【问题描述】:

我有两张桌子,table1table2。 table2 外键(fk 列)是 table1 主键(id 列)。 table2.column3 是 (真/假)。 我想计算 table2.column2 if table2.column3 True 的总和,并将 sum 的结果保存在 table1 的 id equals table2 外键的行中。

这可能吗?可以问一下吗?

【问题讨论】:

  • 请写下您的查询试用

标签: c# sql sum conditional-statements


【解决方案1】:

试试这个:

UPDATE table1 
SET table1.field = 
(SELECT SUM(table2.column2)
FROM table2
WHERE table2.fk_table1 = table1.id
AND table2.column3 = 1) <-- Usually true = 1, false = 0 and tinyint or equivalent type is the used type.

【讨论】:

  • 谢谢你,你是我的英雄 ;) ;D
  • @BehnamAzimi:祝你有美好的一天;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-23
  • 1970-01-01
  • 1970-01-01
  • 2018-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多