title: MySql using a while loop inside a function
body: <p>Given a number, how can I loop through a set of numbers based on that number, inside a MySql function?</p>
  
  <p>To get a clearer picture, pls see my question where I asked how something like this could be done in php. <a href="https://stackoverflow.com/questions/16776479/how-can-i-do-something-like-this-loop-thru-an-indefinite-set-of-numbers">Question here.</a></p>
  
  <p>I'll be doing <code>select myfunction(thenumber)</code>. Based on that number, I got to loop.</p>
  
  <p>As of now,</p>
  
  <pre><code>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 
  </code></pre>
  
  <p>Here's the function I've been fiddling with.</p>
  
  <pre><code>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  &gt;= 1 DO //I'm stuck here
  SET  str = CONCAT(str,x,',');
  SET  x = x - 1; 
  END WHILE;
  RETURN str;
  END
  </code></pre>
  
  <p>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?</p>
  
  <p><strong>Edit</strong></p>
  
  <p><em>Result</em></p>
  
  <p>What I'm trying to get out in the end are links.</p>
  
  <pre><code>So if `thenumber = 1`
  `&lt;a href=foo.php?id=1&gt; &lt;a href=foo.php?id=4&gt; &lt;a href=foo.php?id=7&gt;` //3 Links Output in a single row
  If `thenumber = 2`
  `&lt;a href=foo.php?id=3&gt;` //Same as above. Output in a single row.
  If `thenumber = 10`
  `&lt;a href=foo.php?id=10&gt;`
  </code></pre>