select a.pcno,a.no1,b.numname,sum(a.qty) as qty,b.price,sum(a.qty)*b.price as total from  (select pcno,no1,sum(qty) as qty from cbinputdata where (indate between '2006-1-15' and '2006-4-15') and pcno='AMP2188' group by pcno,no1
union all 
select pc,no1,sum(qty) as qty from cbinputdata1 where  (indate between '2006-1-15' and '2006-4-15') and pc='AMP2188' group by pc,no1) a left join cbgx b on a.pcno=b.pono and a.no1=b.num group by a.pcno,a.no1,b.numname,b.price order by a.pcno,a.no1
现解释一下该语句的使用:有三个表cbinputdata,cbinputdata1,cbgx
cbinputdata,cbinputdata1表是一样的,只是数据录入方式不一样,所以得连接起来,cbinputdata中有30万多条数据,cbinputdata1中有2万多条,cbgx保存单价资料,有1万多条,请各位帮我看看,超时的原因是什么,不过有时我将服务器重新启动一下好像快了一些,是否sql server需要安装补丁什么,请帮忙,谢谢!

解决方案 »

  1.   

    三表查询一般要用关键字关联,如三表都有相同的关键字id,在条件中加
    where a.id=b.id and b.id=c.id
    或另一情况,a,b有相同的关键字id,b,c有相同的关键字id1
    where a.id=b.id and b.id1=c.id1
    否则结果将是三表的迪卡尔乘积20W*2W*1W=2万亿
      

  2.   

    这种情况下的关联会造成数据表相当庞大,所以会超时,解决方案1、先把3个表中要查询的数据过滤到临时表中,然后再通过LEFT JOIN关联
    2、加大TADOQuery的TimeOut属性,譬如设置为30000甚至0建议采用方案1