求sql语句(50分)

  • 主题发起人 主题发起人 月河
  • 开始时间 开始时间

月河

Unregistered / Unconfirmed
GUEST, unregistred user!
sql数据库<br>表1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;表2<br>field1 &nbsp;field2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;field1 &nbsp;field2<br>a1 &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a1 &nbsp; &nbsp; &nbsp;2 &nbsp;<br>a2 &nbsp; &nbsp; &nbsp; &nbsp;2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a1 &nbsp; &nbsp; &nbsp;3<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a2 &nbsp; &nbsp; &nbsp;1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a1 &nbsp; &nbsp; &nbsp;4<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a2 &nbsp; &nbsp; &nbsp;5<br>用update 语句,要求表1的结果为<br>表1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>field1 &nbsp;field2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>a1 &nbsp; &nbsp; &nbsp; &nbsp;10 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>a2 &nbsp; &nbsp; &nbsp; &nbsp;8
 
什么算法阿,看不懂阿。
 
看不懂,如果结果是<br>field1 &nbsp;field2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>a1 &nbsp; &nbsp; &nbsp; &nbsp;10 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>a2 &nbsp; &nbsp; &nbsp; &nbsp;8<br>或<br>field1 &nbsp;field2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>a1 &nbsp; &nbsp; &nbsp; &nbsp;9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>a2 &nbsp; &nbsp; &nbsp; &nbsp;6<br>还有点法
 
update 表1 <br>set <br>&nbsp; field1= field1+ (select sum(field2) from &nbsp;表2 where 表2.field1 &nbsp;=表1.field1 ),<br>&nbsp; field2= field2+ (select sum(field2) from &nbsp;表2 where 表2.field1 &nbsp;=表1.field1 )
 
这个很简单,把原数据库发给我,我来作 &nbsp;QQ 136676485
 
顶dgtg0710的<br>update 表1 <br>set <br>&nbsp; field1= field1+ (select sum(field2) from &nbsp;表2 where 表2.field1 &nbsp;=表1.field1 ),<br>&nbsp; field2= field2+ (select sum(field2) from &nbsp;表2 where 表2.field1 &nbsp;=表1.field1 )
 
这个好像并不难的<br>但似乎写到一层好像有点难了<br>我觉得还是多层好点<br>with query1 do<br>begin<br>&nbsp; close;<br>&nbsp; sql.clear;<br>&nbsp; sql.add('select * from 表1');<br>&nbsp; open;<br>&nbsp; while not eof do<br>&nbsp; begin<br>&nbsp; &nbsp; strField1:=FieldByName('Field1').asstring;<br>&nbsp; &nbsp; intField2:=FieldByName('Field2').asinteger;<br>&nbsp; &nbsp; with Query2 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; close;<br>&nbsp; &nbsp; &nbsp; sql.clear;<br>&nbsp; &nbsp; &nbsp; sql.add('select sum(Field1) as S from 表2 where Field1=:Field1');<br>&nbsp; &nbsp; &nbsp; parameters.paramValues['Field1']:=strField1;<br>&nbsp; &nbsp; &nbsp; open;<br>&nbsp; &nbsp; &nbsp; intS:=FieldByName('S').asinteger;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; with Query2 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; close;<br>&nbsp; &nbsp; &nbsp; sql.clear;<br>&nbsp; &nbsp; &nbsp; sql.add(update 表1 where Field1=:Field1');<br>&nbsp; &nbsp; &nbsp; parameters.paramValues['Field1']:=strField1;<br>&nbsp; &nbsp; &nbsp; parameters.paramValues['Field2']:=intField2+intS;<br>&nbsp; &nbsp; &nbsp; ExecSql;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; next;<br>&nbsp; end;<br>end;<br>写的有点罗嗦,不过肯定是能实现的
 
update 表1 set 表1.field2=表1.field2+b.field2Sum<br>from 表1,(select field1,sum(field2) as field2Sum from 表2 group by field1) as b <br>where 表1.field1=b.field1
 
晕,就是个分组求和,搞这么复杂
 
update Table1<br>set Table1.Field2 = Table1.Field2 + T2.F2<br>from Table1 T1<br>left join (<br>select field1 as F1,sum(isnull(field2,0)) as F2<br>from Table2<br>group by Field1<br>) T2 on Field1 = T2.F1<br><br><br>很簡單一個SQL語句就搞定啦
 
就一分組求和,難道樓主都沒學過SQL
 
就是第一个表的Field2值与第二个表里的同Field1的值加起来阿
 
一个u连,一个select f1 ,sum(f2) group by f1
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
846
SUNSTONE的Delphi笔记
S
S
回复
0
查看
778
SUNSTONE的Delphi笔记
S
D
回复
0
查看
846
DelphiTeacher的专栏
D
后退
顶部