title: MySQL query to get last record in each category
body: <p>I got two tables: <strong>articles</strong> and <strong>categories</strong> (about 20 categories), and I want to get the lastest 10 articles, but no more than one from a category.</p>
  
  <p>Table <strong>Articles</strong></p>
  
  <pre><code>id | title | text
  </code></pre>
  
  <p>Table <strong>Categories</strong></p>
  
  <pre><code>id | name
  </code></pre>
  
  <p>Table <strong>ArticlesCategories</strong> (relationsl table between Articles and Categories)</p>
  
  <pre><code>article | category
  </code></pre>
  
  <p>Im using the query below but the problem is that the 10 results are not all the latest articles.</p>
  
  <pre><code>SELECT id, title, categoryId, categoryName
  FROM ( 
          SELECT a.id, a.title, ac.category AS categoryId, c.name AS categoryName
          FROM articles AS a 
              LEFT JOIN articles_categories AS ac ON ac.article = a.id 
              LEFT JOIN categories AS c ON c.id = ac.category 
          WHERE ac.priority = 1 
          ORDER BY a.id DESC ) AS tmp_table 
  GROUP BY categoryId LIMIT 10
  </code></pre>