title: MySQL variables in nested subqueries
body: <p>Currently I'm working on kind of e-learning platform which contain courses and exams module. The idea is that course is followed by exam. User can solve exam few times (depending on setting in exam table). I have a module where I need to determine whether user should be redirected to exam or statistics page. If user hasn't use all his attempts he should be redirected to exam, otherwise to stats.</p>
  
  <p>So here is my query (little simplified, since all conditions and joins of outer query doesn't matter here) which should determine where to go.</p>
  
  <pre><code>SELECT 
   @course_id := courses.id as id,
   IF(
        (SELECT X.attempts_count FROM
             (SELECT
             COUNT(exams_attempts.id) as attempts_count,
             @max_attempts := exams.max_attempts
             FROM exams
             LEFT JOIN exams_attempts ON exams.id = exams_attempts.quiz_id
             JOIN users ON exams_attempts.user_id = users.id
             WHERE exams_attempts.user_id = 12
             AND exams_attempts.course_id = @course_id
             HAVING attempts_count &gt;= @max_attempts) as X
        ),
        'stats',
        'exam'
   ) as redirect 
  FROM courses
  WHERE courses.id = 1
  </code></pre>
  
  <p>For testing reasons I limited courses to a static value, but in actual usage it's big amount of courses to look up.</p>
  
  <p>And finally - I discovered that this works on localhost, but not on server, despite the same MySql version. I would like to know if there is some setting that prevents my query from being executed properly. Also I would like to know what do you suggest, maybe my idea is not good and I could rebuild that query?</p>