我有两张关联表 
表A
ID    Name  Price  Num GXid
1      a     100   100 1
2      a     50    200 2表B
ID    Name  Price  Num
1      a     50    100
2      a     10    200表A 中的GXID是表B中的ID  
我想查 表A中的Price要减去相关联的表B中的Price然后再乘以表A中的Num 把这给一个别名(money) 求出表A中Name为a的sum(money) 怎么样做 我做到了 把money查出来了 但是 sum()中不能包含子查询 我不想用join连接  可以做到吗?

解决方案 »

  1.   

    select SUM((a.price-b.price)*a.Num)
    from A a,B b 
    where a.GXid=b.ID and a.Name = 'a'不行吗?
      

  2.   

    为什么不想用join呢,你是觉得它慢吗?给GXID加索引不就行了
      

  3.   

    select  sum(money) from (select (a.price - b.price)*a.Num) money from a,b where a.GXid = b.ID)
      

  4.   

    select sum(money) from (select (a.price - b.price)*a.Num) money from a,b where a.GXid = b.ID and a.Name = 'a')少看一条件
      

  5.   

    from a,b where a.GXid = b.ID和 join 有什么区别吗?
      

  6.   

    select sum(money) from (select (a.price - b.price)*a.Num) money from A a,B b where a.GXid = b.ID and a.Name = 'a')
      

  7.   

    既然已经查出来了,可以这样做,把查出money的语句做成一个视图,然后这样查
    select sum(money)  from 视图 where name = 'a',这样就OK了