i am using the following query to select the previous stock based on some between dates, but the following query fails if the auto incremented id is not in order (while delete some record, it has gap),
SELECT in_stock, price
FROM $tablename
WHERE id=(SELECT (id-1)
FROM $tablename
WHERE sale_date BETWEEN '$from_date' AND '$to_date'
ORDER BY entered_date ASC
LIMIT 1)
help me to select the previous in_stock