问一个较为棘手的问题,我试了几种办法,但效果不是很好!

cmt123 2003-04-29 08:23:40
table1 str_C
G_Code G_Name G_Amount //字段名
a1 b1 23
a2 b2 32
. . .
. . .

Table2 STR_C
GCode GName GAmount //字段名
a1 b1 2
a1 b1 3
a2 b2 4
. . .
. . .

现在是判断Table2 中的GCode字段中的每一行, 如果与Table1中的G_Code字段的值相等,那么把Table2表中GCode字段对应的GAmount字段的值加到Table1表中G_Code字段对应的G_Amount中。(注:Table1表中的记录较多,我试了几种方法但速度不是很理想)

...全文
31 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
coolkenmester 2003-04-29
  • 打赏
  • 举报
回复
update table1
set G_Amount=table1.G_Amount+t2.GAmount
from table1 table1,
(select GCode,sum(GAmount) as GAmount from Table2 group by GCode) as Table2
where table1.G_Code=Table2.GCode
宸瑜 2003-04-29
  • 打赏
  • 举报
回复
对table1 表建立G_Code 索引,或者两个都对G_Code 建立索引都可
Rewiah 2003-04-29
  • 打赏
  • 举报
回复
提高速度:
两个表都对gcode建立索引
Rewiah 2003-04-29
  • 打赏
  • 举报
回复
update t1
set gamount=t1.gamount+t2.gamount
from table1 t1,
(select gcode,sum(gamount) as gamount from table2 group by gcode) as t2
where t1.gcode=t2.gcode
cmt123 2003-04-29
  • 打赏
  • 举报
回复
但是楼上的:上面给我的解答在SQL SERVER 内运行通过,可是在Dephi中运行通不过。在Dephi中这条Sql 语句如何写?

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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