funbinod Posted May 9, 2014 Share Posted May 9, 2014 i am trying to command mysql to sum until it reaches certain value from my table 'purchase', for each 'sid' starting from the last row, i need sum of 'pqty' until the result equals a value from string (but to try i've given a certain value). let me define with the rows from my table--- the rows for 'sid=1' from 'purchase' are like this--- date pqty prate pamt 2014/04/29 5 38000 190000 2014/05/04 1 38000 38000 2014/05/13 20 35000 700000 2014/05/19 1 38000 38000 from this row, starting from the last row i want to 'sum(pqty) until it reaches 19(for now). it is achieved from adding last 2 rows(for 19). and stop sum here and return valus or sum of 'pqty', 'prate' and 'pamt'. to achieve this i tried the following SELECT date, pqty, @total := @total + pqty AS total FROM (purchase, (select @total :=0) t) WHERE @total<19 AND sid = $sid ORDER BY date DESC but it's not working. please guide me through this. also suggest something else if this is not the good technique for my purpose. thankz in advance..... Link to comment Share on other sites More sharing options...
justsomeguy Posted May 9, 2014 Share Posted May 9, 2014 If you're going to add logic like that, I think you need to use a stored procedure. You could also return all records and do the sum in PHP. Link to comment Share on other sites More sharing options...
funbinod Posted May 10, 2014 Author Share Posted May 10, 2014 thank u @justsomeguy! but i know nothing about stored procedure. can u please tell me where can i read this with step by step explanation!? Link to comment Share on other sites More sharing options...
davej Posted May 10, 2014 Share Posted May 10, 2014 See... http://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-stored-procedures.html Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now