Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to use limit with variable in MySQL

Tags:

mysql

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;

enter image description here

like image 506
Sheraz Avatar asked Sep 27 '22 09:09

Sheraz


1 Answers

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;
like image 149
Gurkan Yesilyurt Avatar answered Oct 09 '22 06:10

Gurkan Yesilyurt