title: How can I loop through data in mySQL and join tables based on a condition?
body: <p>I'm trying to create a scorecard application and I have 3 separate MySQL tables that I want to <code>INSIDE JOIN</code> based on the <code>Round_ID</code> (keeping all rows with the same <code>Round_ID</code> together).</p>
  
  <p>I've figured out how to join based on this, but now my idea is to use a WHILE loop to iterate through each <code>Round_ID</code> entered in the database and create a separate JOIN table for each one (1,2,3 etc) so I can have a separate scorecard table for each round entered. I decided to put the statements in a procedure so I can call it later on easier. So far this is what I have -</p>
  
  <pre><code>DELIMITER $$
  CREATE PROCEDURE get_scorecard()
  BEGIN
  DECLARE increment INT;
  DECLARE maxRound INT;
  SET increment = 1;
  SET maxRound = (SELECT MAX(Round_ID) FROM Course_Played);
  WHILE increment &lt;= maxRound
  BEGIN
  SELECT Player_Scores.Round_ID, Players.Player_ID, Players.Player_Name, Player_Scores.Player_Score, Course_Played.Course_ID, Course_Played.Course_Name 
  FROM Players 
  INNER JOIN Player_Scores 
  ON Players.Player_ID = Player_Scores.Player_FK 
  JOIN Course_Played 
  ON Course_Played.Course_ID = Player_Scores.Course_FK
  WHERE Round_ID = increment
  SET @increment = increment + 1;
  END
  END$$
  DELIMITER ;
  </code></pre>
  
  <p>But the code does not work. I keep getting a "check syntax error" and can't figure out where it went wrong. (As a side note, I am new to SQL and new to programming in general). I am also using phpMyAdmin if it matters. </p>
  
  <p>Also, using a loop for this really the best way to accomplish this? I looked at cursors but don't know if that would be applicable in this situation...</p>
  
  <p>Any help is greatly appreciated!!</p>