有四个表:
用户表              银行付费表        邮局付费表             网上付费表
-------          ---------------   ----------------     -------------------   
id               id | paymoney     id | paymoney         id | paymoney
-------          ---------------   ----------------     -------------------
1                 1 |  100         1  | 200              2  | 3002                 1 |  200         2  | 100              1  | 500一个用户可以用多种付费方式付费,一个用户在一种在付费方式中可以有多条付费记录,现在要统计用户的付费情况:
比如要统计出在三种付费方式中用户付费总额在1000元以上的用户

解决方案 »

  1.   

    select id from 用户表
    where
     ((select sum(paymoney) from 银行付费 where id=用户表.id)
    +(select sum(paymoney) from 邮局付费 where id=用户表.id)
    +(select sum(paymoney) from 网上付费 where id=用户表.id)) > 1000试一下吧,不知道是否好用。
      

  2.   

    上面的可以,但是要求每个表中每个人必须有一条记录,否则得出的结果是2的付费总额为NULL
      

  3.   

    修正后的:
    select id from 用户表
    where
     ((case (select count(id) from 银行付费 where id=用户表.id) 
         when 0 then 0
         else (select sum(paymoney) from 银行付费 where id=用户表.id)
      end)
    +(case (select count(id) from 邮局付费 where id=用户表.id) 
        when 0 then 0
        else (select sum(paymoney) from 邮局付费 where id=用户表.id)
      end)
    +(case (select count(id) from 网上付费 where id=用户表.id) 
        when 0 then 0
        else (select sum(paymoney) from 网上付费 where id=用户表.id)
      end)) > 1000
      

  4.   

    还是不行,提示语法错误,我的后台是Mysql+linux
    是不是mysql 服务器版本限制问题
      

  5.   

    哦,我在SQL Server下调试通过,MySql就不熟了,应该没有case...when..end这样语句。
    帮不了你了。
      

  6.   

    To  zzllabc(抱朴子--清心释累,绝率忘情) 你的语句不通用,
    用临时表或表变量来搞定,
    declare @mytable table(id varchar(10),paymoney money)
    insert @mytable
    select * from 银行付费表
    union all
    select * from 邮局付费表
    union all
    select * from 网上付费表select id from @mytable 
    group by id 
    having sum(paymoney)>1
      

  7.   

    手头没有MySql,我的语句在Sql server2000中测试通过,不知MySql支持不支持表变量,你可以用临时表试一下。
    select a.*  into #temp  from (select * from 银行付费表
    union all
    select * from 邮局付费表
    union all
    select * from 网上付费表)  a 
     
    --结果
    select id from #temp  
    group by id 
    having sum(paymoney)>1000
      

  8.   

    TO: hmily1688(没什么好说的) 
    不错的方法,搂主:“我的后台是Mysql+linux”,
    此语法应该也不行的。
      

  9.   

    select id from 用户表
    where
     (IFNULL((select sum(paymoney) from 银行付费 where id=用户表.id),0)
    +IFNULL((select sum(paymoney) from 邮局付费 where id=用户表.id),0)
    +IFNULL((select sum(paymoney) from 网上付费 where id=用户表.id),0) > 1000
      

  10.   

    3.24版以前的MySql不支持Union,不知3.24版以后的支持不支持
    用临时表
    Crteate temporary table  mytemp  type=head select  *  from   银行付费表
    insert  into  mytemp select *  from  银行付费用
    insert into  mytemp  select *  from  邮局付费表
    insert into  mytemp  select *  from 网上付费表
    再执行
    select id   from mytemp group by id having  sum(paymoney)>1000
    --如果MySql连最后这一句也不支持,我就没什么好说的了。
      

  11.   

    To qizhanfeng(glacier),小伙子五个三角了,努力啊。加星
      

  12.   

    写错了
    应该是
    Crteate temporary table  mytemp  type=heap select  *  from   银行付费表
    哪具Head应该改为Heap
      

  13.   

    declare @mytable table(id varchar(10),paymoney money)
    insert @mytable
    select * from 银行付费表
    union all
    select * from 邮局付费表
    union all
    select * from 网上付费表select id from @mytable 
    group by id 
    having sum(paymoney)>1
      

  14.   

    declare @mytable table(id varchar(10),paymoney money)
    insert @mytable
    select * from 银行付费表
    union all
    select * from 邮局付费表
    union all
    select * from 网上付费表select id from @mytable 
    group by id 
    having sum(paymoney)>1000
      

  15.   

    select a.id from 
    (select id,(select sum(paymoney) from 银行付费表 t2 where t1.id=t2.id) m2, 
    (select sum(paymoney) from 邮局付费表 t3 where t1.id=t3.id) m3,
    (select sum(paymoney) from 网上付费表 t4 where t1.id=t4.id) m4 
    from 用户表 t1) a 
    where a.m2>1000 or a.m3>1000 or a.m4>1000 
    or a.m2+a.m3>1000 or a.m2+a.m4>1000 or a.m3+a.m4>1000 or
    a.m2+a.m3+a.m4>1000
    方法太笨,有点太牵强了,不过总算能实现,数据量太多可能会慢。
      

  16.   

    to pokemonFK(烟鬼),楼主的是Mysql,MySql支持表变量吗
      

  17.   

    多谢各位支持,帮助,问题这样解决:
    在用户表中加一付费总额字段,每次付费都累加,这样统计就方便了
    这也是没办法的办法,mysql下功能有限
    再次感谢各位