Home > Database > Mysql Tutorial > mysqlsum()函数怎么得不到正确的结果啊!!!!_MySQL

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

WBOY
Release: 2016-06-01 13:02:05
Original
1098 people have browsed it

问题是这样的:一个充值表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次啊!!!!这不是我要的结果啊!!

求正确的语句!!!!!!
Copy after login
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
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template