有四个表:
用户表 银行付费表 邮局付费表 网上付费表
------- --------------- ---------------- -------------------
id id | paymoney id | paymoney id | paymoney
------- --------------- ---------------- -------------------
1 1 | 100 1 | 200 2 | 3002 1 | 200 2 | 100 1 | 500一个用户可以用多种付费方式付费,一个用户在一种在付费方式中可以有多条付费记录,现在要统计用户的付费情况:
比如要统计出在三种付费方式中用户付费总额在1000元以上的用户
用户表 银行付费表 邮局付费表 网上付费表
------- --------------- ---------------- -------------------
id id | paymoney id | paymoney id | paymoney
------- --------------- ---------------- -------------------
1 1 | 100 1 | 200 2 | 3002 1 | 200 2 | 100 1 | 500一个用户可以用多种付费方式付费,一个用户在一种在付费方式中可以有多条付费记录,现在要统计用户的付费情况:
比如要统计出在三种付费方式中用户付费总额在1000元以上的用户
where
((select sum(paymoney) from 银行付费 where id=用户表.id)
+(select sum(paymoney) from 邮局付费 where id=用户表.id)
+(select sum(paymoney) from 网上付费 where id=用户表.id)) > 1000试一下吧,不知道是否好用。
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
是不是mysql 服务器版本限制问题
帮不了你了。
用临时表或表变量来搞定,
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
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
不错的方法,搂主:“我的后台是Mysql+linux”,
此语法应该也不行的。
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
用临时表
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连最后这一句也不支持,我就没什么好说的了。
应该是
Crteate temporary table mytemp type=heap select * from 银行付费表
哪具Head应该改为Heap
insert @mytable
select * from 银行付费表
union all
select * from 邮局付费表
union all
select * from 网上付费表select id from @mytable
group by id
having sum(paymoney)>1
insert @mytable
select * from 银行付费表
union all
select * from 邮局付费表
union all
select * from 网上付费表select id from @mytable
group by id
having sum(paymoney)>1000
(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
方法太笨,有点太牵强了,不过总算能实现,数据量太多可能会慢。
在用户表中加一付费总额字段,每次付费都累加,这样统计就方便了
这也是没办法的办法,mysql下功能有限
再次感谢各位