Monday, October 13, 2014

Using both "sum" and "limit" in a mysql query




Recently I wanted to use both "limit" and "sum" in mysql query.
"Sum" will return single value while "limit" is for returning multiple records.
So, the below query did't work.

select sum(price) as total_price from transactions ORDER by transactiondate DESC limit 50,10

Anyway, I needed this kind of query to get sum of price from all the records starting from 51 to 60 in a pagination script.

I had to redefine the query as below to make it work.

select sum(price) as total_price from ( select price from transactions ORDER by transactiondate DESC limit 50,10 ) as prices



You can subscribe to our Email posts, and you can subscribe to our blog feed.

1 comment:

Josef J said...

Select sum(price) as totalprice from transactions where transactiondate between 50 and 60;



Thanks for visiting our blog


twitterrss facebook linkedin YouTube subscribe


Subscribe via email

Enter your email address: