title: Using sub-query to SELECT MAX value along with LEFT JOIN
body: <p>I have a query for getting search results, which works fine.</p>
  <h3>Example of successful query:</h3>
  <pre><code> SELECT
      individuals.individual_id,
      individuals.unique_id,
      TIMESTAMPDIFF(YEAR,individuals.day_of_birth,CURDATE()) AS age,
      individuals_dynamics.id,
      individuals_achievements.degree
    FROM
      individuals as individuals
    LEFT JOIN
      individuals_dynamics AS individuals_dynamics ON individuals.unique_id = individuals_dynamics.individual_id
    LEFT JOIN
      individuals_achievements AS individuals_achievements ON individuals.unique_id = individuals_achievements.individual_id
    WHERE
      $uuid_access_status $display_type $detailed_search_query
    ORDER BY 
      $search_sort $search_order
  </code></pre>
  <p>From now on, I have more than one record in <code>individuals_achievements</code> per each individual and this is the where I would like to get the MAX value (latest id).</p>
  <p>I tried the many different queries but always was getting an error <em><strong>Call to a member function rowCount() on a non-object.</strong></em></p>
  <p>I understand what that error means but I can't figure out where I'm making that mistake and what is wrong in general.</p>
  <h3>Example of my unsuccessful attempt:</h3>
  <pre><code>  SELECT
      individuals.individual_id,
      individuals.unique_id,
      TIMESTAMPDIFF(YEAR,individuals.day_of_birth,CURDATE()) AS age,
      individuals_dynamics.id,
      individuals_achievements.degree
    FROM
      individuals as individuals
    LEFT JOIN
      individuals_dynamics AS individuals_dynamics ON individuals.unique_id = individuals_dynamics.individual_id
    INNER JOIN
    (
         SELECT
              degree, MAX(id) AS latest_record
         FROM
              individuals_achievements
         GROUP BY
              latest_record
    ) individuals_achievements AS individuals_achievements ON individuals.unique_id = individuals_achievements.individual_id
    WHERE
      $uuid_access_status $display_type $detailed_search_query
    ORDER BY 
      $search_sort $search_order
  </code></pre>
  <p>What am I missing here? Any help please?</p>