Given a number, how can I loop through a set of numbers based on that number, inside a MySql function?
To get a clearer picture, pls see my question where I asked how something like this could be done in php. Question here.
I'll be doing select myfunction(thenumber). Based on that number, I got to loop.
As of now,
If thenumber = 1 then loop backward thru 1,4,7 only
If thenumber = 2 then loop thru 3
If thenumber = 3 then loop thru 10
Here's the function I've been fiddling with.
CREATE DEFINER=`root`@`localhost` FUNCTION `whileloop`(`danum` VARCHAR(2050))
RETURNS varchar(1500)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE x INT;
DECLARE str VARCHAR(255);
SET x = danum;
SET str = '';
#If x = 1, while = 1, 4, 7////////
WHILE x >= 1 DO //I'm stuck here
SET str = CONCAT(str,x,',');
SET x = x - 1;
END WHILE;
RETURN str;
END
How can I do inside a MySql function, what I did php. In php, we stored things in an array. How can we do this here?
Edit
Result
What I'm trying to get out in the end are links.
So if `thenumber = 1`
`<a href=foo.php?id=1> <a href=foo.php?id=4> <a href=foo.php?id=7>` //3 Links Output in a single row
If `thenumber = 2`
`<a href=foo.php?id=3>` //Same as above. Output in a single row.
If `thenumber = 10`
`<a href=foo.php?id=10>`
<a href=id=1> <a href=id=4> <a href=id=7>1it's going be3 linksin one single row output. For the rest, it's only a single link.varcharoutput.