title: SQL: how to get views count from multiple articles sorted by category?
body: <p>I use MySQL and I have a code that gets last 6 articles from each category:</p>
  
  <pre><code>select a.*
  from article a
  where a.created &gt;= coalesce((select a2.created
     from article a2
     where a2.category = a.category
     order by a2.created desc
     limit 5, 1
    ), a.created
   );
  </code></pre>
  
  <p>Now I need also get total views for each article from another table. How to do that? This does not work:</p>
  
  <pre><code>select a.*, Count(view.*) as CountViews
  from article a
  where a.created &gt;= coalesce((select a2.created
     from article a2
     where a2.category = a.category
     order by a2.created desc
     limit 5, 1
    ), a.created
   ) left join view on a.id = view.post_id;
  </code></pre>
  
  <p>Example of printing articles by categories: <a href="https://nedvigimostmsk.ru/" rel="nofollow noreferrer">https://nedvigimostmsk.ru/</a></p>