2

I have the following small subquery in query in stored procedure.

(select f_cnt from results limit (i-1)*10,i*10)

But there is the syntax error:

"(" is not valid at this position, expecting an identifier

So, the question is: do I have a possibility to use brackets and/or arithmetic operators in LIMIT clause?

Documentation says I can use local variables in LIMIT clause within stored procedure. Do I really need to declare and set different variables for this case?

Just in case, link for the code of stored procedure.

1 Answer 1

2

You can't do arithmetic at that point

So do instead

SET @sql := CONCAT("SELECT * FROM TEsttable WHERE id In(select f_cnt from results limit ",(i-1) * 10,",",i*10,")");
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Which will produce

SELECT * FROM TEsttable WHERE id In(select f_cnt from results limit 90,100)
Sign up to request clarification or add additional context in comments.

3 Comments

ehm... Ok, I think I understood what kind of mechanics you're suggesting. But I don't yet know how these statements work. Actually, my topic is not an issue, because I've passed through syntax error with two additional variables. Still, the question about LIMIT clause is open.
@Gennadiy prepared statemenets like this work like any other prepared statement, you can add to the query where you want data a ? and add to the execute part the variables see dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html only Limit and offet doesn't work that way, it has to be done like teh answer
it looks like useful approach, I'll learn deeper. Thanks

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.