例如表1
字段: id zc zj gl
表2
字段:  id lx zf df
表3
字段: id gb gbje
公式  (表1.zc+表2.lx+表2.df)/gb
gb首先提取表3.gb,若表3.bg为空值或者为0,则提取表3.gbje.最后将结果显示到edit中

解决方案 »

  1.   

    select (t1.zc+t2.lx+t2.df)/(case when t3.gb is null or t3.gb=0 then t3.gbje else t3.gb end) rst
    from t1,t2,t3 where t1.id=t2.id and t1.id = t3.id
      

  2.   

    谢谢!我想把这个sql语句放在adoquery的sql字段中运行,但是报错说“无法绑定由多个部分组成的标示符‘t2.id’”。请问如何解决...
      

  3.   

    --我的是oracle 数据库,sqlserver应该不需要改动sql,如果是access需要修改case语句access不支持case语法--测试数据
    create table t1(id int, zc int, zj int, gl int);
    create table t2(id int, lx int, zf int, df int);
    create table t3(id int, gb int, gbje int);
    insert into t1 values(1,1,1,1);
    insert into t2 values(1,2,2,2);
    insert into t3 values(1,2,0);
    insert into t1 values(2,1,1,1);
    insert into t2 values(2,2,2,2);
    insert into t3 values(2,0,3);
    --执行查询
    select (t1.zc+t2.lx+t2.df)/(case when t3.gb is null or t3.gb=0 then t3.gbje else t3.gb end) rst
    from t1,t2,t3 where t1.id=t2.id and t1.id = t3.id
    --查询结果
    Rst
    2.5
    1.66666666666667