欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

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

程序员文章站 2022-04-28 22:55:38
...
问题是这样的:一个充值表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
相关标签: 函数 结果