当前位置:Gxlcms > mysql > mysqlsum()函数怎么得不到正确的结果啊!!!!_MySQL

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

时间:2021-07-01 10:21:17 帮助过:8人阅读

问题是这样的:一个充值表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

人气教程排行