这样的sql语句怎么写?

bhut 2003-08-21 02:55:52
这样的sql语句怎么写?
从两个表分别查出符合一定条件的值的总和再相加,
把结果付给另一个表的一个字段。要求用一条语句实现
比如分开查询
select sum(col2) from table2 where table2条件 的结果为a
select sum(col3) from table3 where table3条件 的结果为b
希望得到a+b
我现在写的语句
update table1 set col1=
(select sum(col2)+sum(col3) from table2,table3
where table2条件 and table3条件) where……
得到的结果不正确,因为符合table2条件和table3条件的记录数不

等。
上面那条语句的结果却不是a+b
...全文
21 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
CrazyFor 2003-08-21
  • 打赏
  • 举报
回复
update table1 set col1=(select sum(col2) from table2 where table2条件)+(select sum(col3) from table3 where table3条件)
where……
pengdali 2003-08-21
  • 打赏
  • 举报
回复
update table1 set col1=(select sum(col2) from table2 where table2条件)+(select sum(col3) from table3 where table3条件) where……
pengdali 2003-08-21
  • 打赏
  • 举报
回复
update table1 set col1=(select sum(col2) from table2 where table2条件)+(select sum(col3) from table3 where table3条件) where……
lifeforu 2003-08-21
  • 打赏
  • 举报
回复
update table1 set col1=(select sum(col2) from table2 where table2条件) +
(select sum(col3) from table3 where table3条件)

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧