问题
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