title: Mysql alternative for LIMIT inside subquery in mysql 5.1.49
body: <pre><code>SELECT student_id FROM `students` AS s1
  WHERE student_id IN 
    (SELECT s2.student_id FROM `students` AS s2
       WHERE s1.year_of_birth = s2.year_of_birth
       LIMIT 10)
  </code></pre>
  
  <p>Can't process this query on my server. It drops errors, that says that this version of mysql doesn't support limit inside subqueries etc(ERROR 1235).</p>
  
  <p>Is there any solution for my version of mysql 5.1.49?</p>
  
  <pre><code>SELECT
      id,
      region
  FROM (
      SELECT
          region,
          id,
          @rn := CASE WHEN @prev_region = region
                      THEN @rn + 1
                      ELSE 1
                 END AS rn,
          @prev_region := region
      FROM (SELECT @prev_region := NULL) vars, ads T1
      ORDER BY region, id DESC
  ) T2
  WHERE rn &lt;= 4
  ORDER BY region, id
  </code></pre>
  
  <p>Thanks to <strong>Mark Byers</strong></p>