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.