4

How to use LIMIT instruction with parameters in MySQL?

Like:

SELECT * FROM someTable LIMIT variable1, variable2;

2
  • 1
    Must the answer be purely SQL, or are you using a web development language, such as PHP or ASP.NET? Commented Feb 4, 2010 at 6:42
  • Purely SQL, working in MySql. Commented Feb 4, 2010 at 6:53

1 Answer 1

6

Try this inside SP

SET @String1 = concat(concat(concat(concat("SELECT field1,field2 FROM table1 WHERE field3 = ",sp_var1," && field4 = "),sp_var2," LIMIT "),sp_Var_skip,","),sp_var_count); 
PREPARE Stmt FROM @String1; 
EXECUTE Stmt; 
SET @String1 = "" 

Alternatively

DELIMITER $ 
CREATE PROCEDURE `tmp`() 
BEGIN 
PREPARE STMT FROM "SELECT * FROM users LIMIT ?,?"; 
END$ 
DELIMITER; 

SET @a=2; 
SET @b=1; 

CALL tmp(); 
EXECUTE STMT USING @a, @b; 

More information on this here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.