各位大师,请教ADOQuery1的SQL语句怎么写:
表结构Table1,有2列
a b
1 1
1 2
1 1
1 1
1 2
2 3
2 4
2 2
2 1
2 5
另外还有一个表Table2,结构同Table1.
想得到Table2的内容为
a b c
1 7
2 15
就是说,表1中a列相同的记录在表2中只出现一次,表2的b列是表1中对应的相同a的b列的和(即7=1+2+1+1+2,15=3+4+2+1+5),如何实现? With ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add(' update table1,table2 set table2.b = ');
SQL.Add(' (select sum(b) from table1 group by a) ');
SQL.Add(' where table1.a= table2.a');
ExecSQL;
end;
这样写代码,报错:语法错误(操作符丢失)在查询表达式‘table1.a= table2.a’中。
表结构Table1,有2列
a b
1 1
1 2
1 1
1 1
1 2
2 3
2 4
2 2
2 1
2 5
另外还有一个表Table2,结构同Table1.
想得到Table2的内容为
a b c
1 7
2 15
就是说,表1中a列相同的记录在表2中只出现一次,表2的b列是表1中对应的相同a的b列的和(即7=1+2+1+1+2,15=3+4+2+1+5),如何实现? With ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add(' update table1,table2 set table2.b = ');
SQL.Add(' (select sum(b) from table1 group by a) ');
SQL.Add(' where table1.a= table2.a');
ExecSQL;
end;
这样写代码,报错:语法错误(操作符丢失)在查询表达式‘table1.a= table2.a’中。
from table1 group by a 大致应该如此
begin
Close;
SQL.Clear;
SQL.Add(' select a, sum(b) into table2 from table1 group by a ');
ExecSQL;
end;OLE automation内部错误。
如果你一定要将这个结果集插入到table2中,然后从table2中取结果也可以。insert into table1
select a,sum(b) b from table1 group by a然后直接
select * from table2
select a,sum(b) b from table1 group by a
报错:
工程project1.exe检测到错误类eoleexception,错误:‘OLE Automation内部错误’‘。进程中止,使用单步或运行继续运行