日期:2014-05-18  浏览次数:20423 次

50分!在线等!这个更新查询语句该怎么写?
table1
....字段a       面积     .....
....001           100         .....
....002           150         .....
....001           200         .....  

table2
....字段b       合计面积     .....
....001           300               .....
....002           150               .....

请问更新语句该怎么写?
update   table2   set   合计面积=????

------解决方案--------------------
update table2 set 合计面积=t.面积
from table2
inner join (select 字段a,sum(面积) as '面积 ' from table1 group by 字段a)t on t.字段a=table2.字段b
------解决方案--------------------
update table2 set 合计面积=(select sum(面积) from table1 where 字段a=table2.字段b)
------解决方案--------------------
--試試
update table2 set 合计面积=(select sum(面积) from table1 t where 字段a=t.字段a)
------解决方案--------------------
使用语句嵌套就OK了

合计面积应该等于 select sum(面积) from table1,table2 where table1.字段a=table2.字段b

或者直接使用程序先读出来所有在table2里面存在的字段b,然后到table1里面寻找字段b和字段a相等的数据,求和再输入到table2中
------解决方案--------------------
錯了,改改
update table2 set 合计面积=(select sum(面积) from table1 where 字段a=table2.字段a)
------解决方案--------------------
update table2 set 合计面积=(select sum(面积) from table1 where 字段a=table2.字段b)
------解决方案--------------------
也寫一個,:)

Update
A
set
合计面积 = B.面积
From
table2 A, (Select SUM(面积) As 面积 From table1 Group By 字段b) B
Where A.字段a = B.字段b

------解决方案--------------------
feixiangVB(別以爲我不在乎!) ( ) 信誉:100 Blog 2007-03-27 10:39:31 得分: 0


錯了,改改
update table2 set 合计面积=(select sum(面积) from table1 where 字段a=table2.字段a)


Top
li1229363() ( ) 信誉:100 Blog 2007-03-27 10:40:17 得分: 0


sum(table1.面积)

上面的有错误


=========================================
這裡是沒有問題的,後面條件有問題,已改
------解决方案--------------------
update table2
set 合计面积 = t.合计面积
from table2,
(
select 字段a , sum(面积) as 面积 from table1 group by 字段a
) t
where table2.字段b = t.字段a
------解决方案--------------------
update table2 set 合计面积=(select sum(面积) from table1 where 字段a=table2.字段b)


update table2 set 合计面积=t.面积
from table2
inner join (select 字段a,sum(面积) as '面积 ' from table1 group by 字段a)t
on t.字段a=table2.字段b

还有楼上的的都对,
个人觉得还是第一个最好!