比如 select px from table1 where s1 in (select a1 from table2) 
查出来的是一组记录,要对这组的px求和
怎么调用阿?
还有,如何动态建立索引?如果该字段有重复记录会不会不允许建立索引?
能给出代码吗?

解决方案 »

  1.   

    select px from table1 where s1 in (select a1 from table2) 
    写成  select sum(px) from table1 where s1 in (select a1 from table2) !!!!
      

  2.   

    如在Delphi中,可以
    aQuery.First;
    while not aQuery.Eof() do
    begin
      ...
      aQuery.Next;
    end;是不是很笨:)
      

  3.   

    太谢谢啦!不过我还是不太清楚求出来的sum结果怎么取阿?
    能赋给一个变量吗?
    还有索引的问题呢?建立索引应该能加快速度吧?我的表特别大阿
    “如何动态建立索引?如果该字段有重复记录会不会不允许建立索引?”
      

  4.   

    select sum(px) from table1 where s1 in (select a1 from table2) !!!!当然可以给一个变量。
    字段重复不能建立索引的
      

  5.   

    你要求和的这个px是个什么类型的东东啊?
    整数?
    你可以这样:
    select sum(px) as aaa from table1 where s1 in (select a1 from table2) !!!!
    然后
    string1:=trim(adoquery1.fieldbyname('aaa').asstring);
    就可以在程序中用了!
      

  6.   

    http://expert.csdn.net/Expert/topic/2934/2934912.xml?temp=.7470819
    我来求助的;)
      

  7.   

    有又人告我用join比in快(我的表特别大)他给的代码是这样的:
    select table2.a1,table1.px from table1 inner join table2
    on table1.s1=table2.a1 or table1.s2=table2.a1 or table2.s1=table2.a1
    能不能按您教我的直接写成:
    select table2.a1,sum(table1.px)……
    然后如果直接写入表2里,就是一个a1对应一个sum(px),还想和表中原有的px相加,怎么写呢?
    对了,as是指定别名吧?