user3423909
user3423909

Reputation: 189

Mysql retrieving range between 2 max values

I'm trying to retrieve a range of values between the last row in a table and the row 40 rows above it: 2568, 2567, 2566, etc. The query I'm using is not getting it done:

SELECT * FROM posts 
WHERE front_weight 
BETWEEN 'MAX(front_weight)-40' AND 'MAX(front_weight)'

Upvotes: 1

Views: 45

Answers (2)

Iłya Bursov
Iłya Bursov

Reputation: 24156

try this one:

SELECT * FROM posts 
WHERE front_weight 
order by front_weight desc
limit 40

instead of front_weight I suppose you have to use some kind of id column

Upvotes: 1

Max
Max

Reputation: 488

From your wording I assume front_weight is unique? If so you can use:

SELECT * FROM posts
ORDER BY front_weight DESC LIMIT 40

Upvotes: 2

Related Questions