mysql get difference instead of SUM
问题 I have the following query : SELECT SUM(P_QTY) FROM rankhistory WHERE P_ID= '1' AND RH_DATE>=1438556400 AND RH_DATE<1438642800 The above query returns 268 The result set contains two elements of P_QTY which are 160 and 108 Now what I want is be able to receive the difference instead of the sum, so what I want my query to return is 52 , how can I achieve that through sql query? Please note that the subquery can return more than one result, and the intended is get the total change. For example