排版有点问题,补个~t1:  id orderid per  ischeck 
      1   1001   a     true 
      2   1002   b     true 
      3   1003   c     false 
      4   1004   d     true t2:  id orderid(外键约束t1的orderid字段)  money 
      1            1001                   10 
      2            1002                   10 
      3            1001                   10 
      4            1001                   10 
      5            1002                   10 问题:我想通过一个sql取出类似这样的结果: 
  t1.orderid  t2.moey(sum相应的t2.orderid的结果)  t1.ischeck(这里是筛选条件,只显示结果为true的) 
      1001                30                                      true 
      1002                20                                      true 
      ... 

解决方案 »

  1.   

    select a.orderid,a.ischeck,
    b.money
    from t1 a join
    (select orderid,sum(money) money from t2 group by orderid) b 
    on a.orderid=b.orderid and a.ischeck=true--or
    select a.orderid,a.ischeck,sum(b.money) money from t1 a
    join t2 b on a.orderid=b.orderid and a.ischeck=true
    group by a.orderid,a.ischeck
      

  2.   


    select t1.orderid, summoney,t1.ischeck from t1,(select sum(money)as summoney from t2 where t2.orderid = t1.orderid)b
    where ischeck = true