How to select Range of rows based on field values - MySQL

我的梦境 提交于 2019-12-08 13:14:56

问题


I don't even know if this possible. Will do my best to explain the question.

Here's the data:

The red outlined column is the main one I'm trying to work with. These are Revs.

I need to select the RANGE OF ROWS between the Max(revs) - 100,000. All the rows in between 46800613 and 47800613 basically. I have no idea how to do this. Because this data is added to about every few seconds - the amount of rows may vary.

Any ideas on how to pull the Range in between the highest rev and 100,000 less of that without calculating the fields at this point? ANY help is greatly appreciated.

Thanks!

P.S. Please let me know if I haven't explained this well. It's been a little frustrating to get my mind around this one.


回答1:


You can use a subquery to get the maximum and then just use a where clause:

select t.*
from t cross join
     (select max(revs) as maxrev from t) x
where t.revs >= x.maxrev - 100000;

I would strongly advise you to have an index on revs.



来源:https://stackoverflow.com/questions/34547808/how-to-select-range-of-rows-based-on-field-values-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!