I’m using MySQL to store financial stuff, and using the data to build, among other things, registers of all the transactions for each account. For performance reasons – and to keep the user from being overwhelmed by a gargantuan table – I paginate the results.
Now, as part of the register, I display a running balance for the account. So if I’m displaying 20 transactions per page, and I’m displaying the second page, I use the data as follows:
- Transactions 0 – 19: Ignore them – they’re more recent than the page being looked at.
- Transactions 20 – 39: Select everything from these – they’ll be displayed.
- Transactions 40 – ??: Sum the amounts from these so the running balance is accurate.
It’s that last one that’s annoying me. It’s easy to select the first 40 transactions using a LIMIT clause, but is there something comparable for everything but the first 40? Something like “LIMIT -40”?
I know I can do this with a COUNT and a little math, but the actual query is a bit ugly (multiple JOINs and GROUP BYs), so I’d rather issue it as few times as possible. And this seems useful enough to be included in SQL – and I just don’t know about it. Does anybody else?
The documentation says:
Next time, please use the documentation as your first port of call.