首页 >数据库 >mysql教程 >mysqlsum()函数怎么得不到正确的结果啊!!!!_MySQL

mysqlsum()函数怎么得不到正确的结果啊!!!!_MySQL

WBOY
WBOY原创
2016-06-01 13:02:051090浏览

问题是这样的:一个充值表state=1表示充值成功的,一个消费表state=1表示消费成功的。我要列出充值成功的总值大于消费成功的总值所以用户id,充值总值,消费总值。语句如下:
SELECT a.userid,SUM(a.number) AS mai,SUM(i.total) AS hua
FROM account AS a,info AS i
WHERE a.state=1 AND a.userid=i.userid AND i.state=1
GROUP BY a.userid
HAVING SUM(a.number)>SUM(i.total)
但是充值总值,消费总值错误了。。。
比如充值4次成功。消费3次成功。
列出的充值额是真值*3次。消费额是真值*4次啊!!!!这不是我要的结果啊!!

求正确的语句!!!!!!
SELECT a.userid, a.mai, i.hua FROM (SELECT userid, SUM(number) AS mai FROM account WHERE state=1 GROUP BY userid) AS a, (SELECT userid, SUM(total) AS hua FROM info WHERE state=1 GROUP BY userid) AS i WHERE a.userid=i.userid AND a.mai > i.hua

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn