I am trying this code to use LIMIT in MYSQL with a variable. When I use a simple number for LIMIT it works fine but when I use a variable then it's not working.
SET @increment:=9;
SET @number:=1;
SET @end:=10*@number;
SET @start:=@end-@increment;
SELECT name, detail FROM tab1 LIMIT @start, @end;
When I use the below code (no variable) it works fine:
SELECT name, detail FROM tab1 LIMIT 0,10;
I'm using in this way. Maybe it'll help you.
SET @increment := 4;
SET @number := 1;
SET @end := 5 * @number;
SET @start := @end - @increment;
PREPARE stmt FROM 'SELECT @i := total, user FROM logintable LIMIT ?,?';
EXECUTE stmt USING @start, @end;
SELECT @i AS result;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With