title: (MySQL) Stored Procedure - Looping through result
body: <p>We're migrating and application from PostgreSQL to MySQL.<br>
  Basically, I would like to loop through a result in MySQL.</p>
  
  <p><code>SELECT col1 FROM table1;</code> &lt;--- (1) Get the result from this query.</p>
  
  <pre><code>LOOP THROUGH col1Result
  </code></pre>
  
  <p><code>SELECT myCol FROM table2 WHERE thisCol = col1Result</code> &lt;--- Equal to every single results from the previous query.</p>
  
  <pre><code>END LOOP;
  </code></pre>
  
  <p>I also found this reference<br>
  <a href="http://dev.mysql.com/doc/refman/5.0/en/cursors.html">http://dev.mysql.com/doc/refman/5.0/en/cursors.html</a>
  <br>However, I'm stuck with this line.</p>
  
  <pre><code>FETCH cur1 INTO a, b;
  </code></pre>
  
  <p>Doesn't that get every single result of <code>cur1</code> into variables <code>a</code> and <code>b</code>?
  How would I make sure that I'm currently on the first index of variable <code>a</code>?<br>
  <br>
  Here's an example on how the result will be used on my end (Written in PostgreSQL).</p>
  
  <pre><code>FOR my_record IN
  
     SELECT DISTINCT col1
     FROM            table1
     WHERE           col2 = param1;
  
  LOOP
  
     SELECT DISTINCT col4
     FROM            table2
     WHERE           col3 = my_record.col1;
  
     IF true THEN
        RAISE EXCEPTION '%', 'ERROR MESSAGE' || my_record.col1;
     END IF
  
     SELECT DISTINCT col5
     FROM            table3
     WHERE           col6 = my_record.col1;
  
     IF true THEN
        RAISE EXCEPTION '%', ERROR MESSAGE' || my_record.col1;
     END IF;
  
  END LOOP;
  </code></pre>