“逆”限制?
我使用 MySQL 来存储财务资料,并使用数据来构建每个帐户所有交易的寄存器等。出于性能原因 - 并且为了防止用户被庞大的表格淹没 - 我对结果进行分页。
现在,作为登记的一部分,我显示该帐户的运行余额。因此,如果我每页显示 20 笔交易,并且显示第二页,我将按如下方式使用数据:
- 交易 0 - 19: 忽略它们 - 它们比页面更新被注视。
- 交易 20 - 39: 从其中选择所有内容 - 它们将被显示。
- 交易 40 - ??: 将这些金额相加,以便运行余额准确。
令我烦恼的是最后一个。使用 LIMIT 子句很容易选择前 40 个事务,但是除前 40 个之外的所有事务是否都具有可比性?像“LIMIT -40”之类的东西?
我知道我可以用 COUNT 和一点数学来做到这一点,但实际的查询有点难看(多个 JOIN 和 GROUP BY),所以我宁愿尽可能少地发出它。这似乎足够有用,可以包含在 SQL 中 - 但我只是不知道。还有其他人吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文档说:
下次,请使用文档作为您的第一个停靠点。
The documentation says:
Next time, please use the documentation as your first port of call.
你可以这样破解它:
这有点像在 MySQL 中使用 Oracle 的 rownum 。
You can hack it this way:
It's kinda like having Oracle's rownum in MySQL.