table1
UserId  JinE
1       500.00
1       650.00
2       350.00
3       460.00
4       340.00
4       220.00
5       150.00table2
UserId  BuTie
2       150.00
4       120.00
6       100.00
最终想要得到UserId  Qian
1       1150.00
2       500.00
3       460.00
4       680.00
5       150.00
6       100.00

解决方案 »

  1.   

    说明:
    table2中的UserId是不会重复的。
      

  2.   

    select UserId,
           sum(JintE) as Qian
     from  (select *
              from table1
            union all
            select *
              from table2) t
    group by UserId
      

  3.   

     select UserId,sum(Qian) as Qian from 
    (select UserId,  JinE as Qian 
     from table1
    union all
    select UserId,  BuTie as Qian  from table2 ) tmp
    group by UserID
      

  4.   


    create table tb1
          (userid int,
           jine   float(10)
           )
    create table tb2
          (userid int,
           butie float(10)
           )insert tb1 values(1,500.00)
    insert tb1 values(1,650.00)
    insert tb1 values(2,350.00)
    insert tb1 values(3,460.00)
    insert tb1 values(4,340.00)
    insert tb1 values(4,220.00)
    insert tb1 values(5,150.00)insert tb1 values(2,150.00)
    insert tb1 values(4,120.00)
    insert tb1 values(6,100.00)
    select userid,sum(jine) qian from
    (select * from tb1
    union all
    select * from tb2) b 
    group by userid
    --drop table tb1
    --drop table tb2
    userid  qian
    ------------
    1 1150
    2 500
    3 460
    4 680
    5 150
    6 100
      

  5.   

    declare @t1 table(Userid int,JinE numeric(12,2))
    insert @t1 select 1,500.00
    union all select 1,650.00
    union all select 2,350.00
    union all select 3,460.00
    union all select 4,340.00
    union all select 4,220.00
    union all select 5,150.00declare @t2 table(Userid int,BuTie numeric(12,2))
    insert @t2 select 2,150.00
    union all select 4,120.00
    union all select 6,100.00select userid,Qian=sum(JinE)
    from(
    select * from @t1 union all select * from @t2
        )t
    group by userid/*
    userid      Qian                                     
    ----------- ---------------------------------------- 
    1           1150.00
    2           500.00
    3           460.00
    4           680.00
    5           150.00
    6           100.00(所影响的行数为 6 行)
    */