title: mySQL select latest distinct rows from table
body: <p>I have this table.</p>
  
  <pre><code>id  country     col1    col2    col3
  1   country1    1   2   3
  2   country1    2   2   1
  3   country1    1   3   2
  4   country2    3   2   2
  5   country2    3   3   3
  6   country3    3   2   2
  7   country3    3   1   1
  </code></pre>
  
  <p>I am trying to output the last row of each distinct country.</p>
  
  <pre><code>id  country     col1    col2    col3
  3   country1    1   3   2
  5   country2    3   3   3
  7   country3    3   1   1
  </code></pre>
  
  <p>I have tried various solutions such as:</p>
  
  <pre><code>select distinct(country), col1, col2, col3 from ( SELECT country, col1, col2, col3 from tablename order by id DESC) a limit 1
  </code></pre>
  
  <p>However, I cannot get the required output.</p>
  
  <p>How can I obtain the latest distinct row for each country?</p>
  
  <p>Thank you!</p>