How about something like this:
SELECT @MatchID = ID FROM tbl WHERE Value = 'Yes' ORDER BY ID ASC LIMIT 1; SELECT @Page = CEIL(COUNT(*) / 5) FROM tbl WHERE ID <= @MatchID; SET @Offset = (@Page - 1) * 5; SELECT * FROM tbl ORDER BY ID ASC LIMIT @Offset, 5;
Note. The above does not support @MatchID, which is not found.
I'm not sure MySQL limits should be constant, so if they do, you will have to calculate the offset in PHP or any programming language that you use to connect to MySQL. Alternatively, perhaps this would work instead of the last SELECT statement in the above example:
SET @selectSQL = CONCAT('SELECT * FROM tbl ORDER BY ID ASC LIMIT ', @Offset, ', 5'); PREPARE stmt FROM @selectSQL; EXECUTE stmt;
James source share