如用户表
User
ID  Name
1   小王
2   小张文具销售表
table_wj
ID  UserID  Money
1   1       120
2   2       140
3   2       250
4   1       140
5   1       80日用销售表
table_zy
id  UserID  Money
1   2       140
2   2       170
3   1       200
4   2       210
--------------------
现在要查询出来的效果是:总销售排名
如:
UserID  Money  Sort
2       910    1
1       540    2也就是查出User中的用户在table_wj,table_zy的总销售价,再根据得出来的价钱高低得出排名Sort谢谢

解决方案 »

  1.   

    select a.UserID,sum(a.Money) as Money
    from 
    (
    select UserID,Money from table_wj
    union
    select UserID,Money from table_zy
    ) a
    group by a.UserID
    order by sum(a.Money) desc
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
      

  2.   

    补充一下,这样写有可能遗漏两个表中用户相同价钱也相同的数据,下面写法就没问题
    select a.UserID,sum(a.Money) as Money
    from 
    (
    select UserID,Money from table_wj
    union all
    select UserID,Money from table_zy
    ) a
    group by a.UserID
    order by sum(a.Money) desc
      

  3.   

    把table_wj和table_zy给union起来,再group by的时候sum,楼上给出的没错的了。