Mysql-for help, the union query times and the same field in the same table, the sum result is always incorrect

Source: Internet
Author: User
SELECTso. *, t1. time AS last accounting Time, IFNULL (SUM (t1. amount), 0) AS total amount, IFNULL (SUM (amount), 0) AS order amount, IFNULL (SUM (t3. amount), 0) AS sales order amount & #39 ;. & #39; FROM order table ASsoLEFTJOIN & #39 ;. & #39; (SELECTID, amount, number, time FROM streaming water meter WHERE Type 1 orderbydatatimedesc) t1ON (t1. No. so. order Number) mysqlphp

SELECT so. *, t1. time AS last accounting Time, IFNULL (SUM (t1. amount), 0) AS total amount, IFNULL (SUM (amount), 0) AS order amount, IFNULL (SUM (t3. amount), 0) AS sales order amount'
. 'From order table AS so LEFT join'
. '(Select id, amount, number, time FROM flow meter WHERE type = 1 order by datatime desc) t1 ON (t1. No. = so. order No.) OR (t1. No. = so. order number) left join'
//. '(Select id, amount, number FROM flow meter WHERE type = 1) t2 ON t2. No. = so. Order No. left join'
. 'Streaming water meter AS t2 ON t2. No. = so. Order No. AND t2. type = 1 LEFT join'
. '(SELECT amount, no. FROM flow meter WHERE type = 1 AND subject = "sales order deposit") t3 ON t3. No. = so. order

I want to obtain the total order amount, order amount, and sales order amount based on the order number or order number in the order. All amounts are recorded in the amount of the flow meter, by using the type and number, I tried it for a long time. There is always an error in the sum of the sales amount or the order amount, and there will always be two or three times more, I guess three queries will summarize the query results.
Please advise how to write this joint query.
For ease of understanding, I have changed the table name and field name to Chinese.

Wait until the answer is answered, and you have done it yourself.

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.