title: How to use a select query in a for loop in mysql?
body: <p>I have the following SQL code which works fine on small datasets. I have a friend who told me I should look into putting this into a FOR loop in MySQL using a <code>SELECT DISTINCT c.name</code> as the looping condition.</p>
  
  <pre><code>SELECT c.name, c.limit, c.price, c._low, c._high, c.date
  FROM comp c
  WHERE c.e_id IN 
  (SELECT e_id FROM part_e WHERE core_id=23)
  OR c.pn IN
  (SELECT pn FROM part_ct WHERE core_id=23)
  GROUP BY c.name
  ORDER BY c.date DESC, c.name ASC
  LIMIT 0,200
  </code></pre>
  
  <p>As a new SQL programmer, I was looking up how to do loops in MySQL. While it seems simple enough conceptually, I am having a hard time trying to figure out how to actually design the SQL statement to actually run this in a loop.</p>
  
  <p>My friend told me that the reason you should use a loop is that using 'post' operators on SQL datasets can have negative effects on performance either when dealing with extremely large datasets or when dealing with extremely large query requests.</p>
  
  <p>This makes sense to me, so I am looking for some help in understanding how to do this, and more information as to why subqueries are considered bad practice.</p>
  
  <p>Thanks for your help everyone!</p>