Why can't the mysqlsum () function get the correct result !!!! _ MySQL

Source: Internet
Author: User
The problem is: a full 20540; table state1 indicates a full 20540; successful, and a consumption table state1 indicates a successful consumption. I want to list the total charge 20540; total successful charge 20540; total charge 20540; total charge 20540; total charge 20540; total consumption 2054
The problem is: a recharge table state = 1 indicates that the recharge is successful, and a consumption table state = 1 indicates that the consumption is successful. I want to list the total value of successful recharge is greater than the total value of successful consumption. Therefore, the user ID, total value of recharge, and total consumption value are listed. Statement: SELECT. userid, SUM (. number) AS mai, SUM (I. total) AS huaFROM account AS a, info AS iWHERE. state = 1 AND. userid = I. userid AND I. state = 1 group by. useridHAVING SUM (. number)> SUM (I. total) but the total value of recharge is incorrect... For example, the account is successfully recharged four times. Consumption is successful three times. The value of the value listed is the true value * 3 times. The consumption amount is the true value * 4 times !!!! This is not the result I want !! Correct statement !!!!!!
SELECT. userid,. 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. userid = I. userid AND. mai> I. hua

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.