I’ve got a stored procedure in my MySQL database, and need to figure out how to limit the return to create some pagination.
Some pseudocode:
CREATE PROCEDURE `my_procedure`(IN member_id INT, IN start INT, IN end INT)
BEGIN
SELECT * FROM member_activity WHERE `member_id` = member_id
<if start is not null>
LIMIT start, end
<endif>
END;
If I pass a null value, how do I simply unlimit the query?
Passing my_procedure(1,null,null) returns an error.
I know I can just wrap the entire query in an IF statement, but I’d rather not, because there’s several other variables that would be annoying to keep in sync. Is it possible to accomplish this without writing the entire query twice?
Thanks
As mentioned in the manual:
Since, as you point out, one cannot use the
IFNULL()function within theLIMITclause, prior to yourSELECTcommand you could do: