title: MySQL: How to SELECT only three of each differing x-column-name
body: <p>I want to display my blogs in a very future proof way. So I need to make a query that will reference new blog types if they are created. Hence the x-column-name. In this case that's blogType. This select query should contain the blog information for all blog types, but for each blog type get 3 Blogs. It's kind of confusing so this is why I am reiterating!</p>
  
  <p>I have done a bit of work already in googling how to limit the results. Shown below:</p>
  
  <pre><code> $query = 'SELECT * FROM blogs
                ORDER BY dateWritten
                ASC LIMIT 3';
  </code></pre>
  
  <p>I'll be outputting the results to blog_rss.php using an array and a foreach loop. I get the array from a function like this:</p>
  
  <pre><code>function get_Recent_Blogs() {
      global $db;
      $query = 'SELECT * FROM blogs
                ORDER BY dateWritten
                ASC LIMIT 3';
      try {
          $statement = $db-&gt;prepare($query);
          $statement-&gt;execute();
          $result = $statement-&gt;fetchAll(PDO::FETCH_ASSOC);
          $statement-&gt;closeCursor();
          return $result;
      } catch (PDOException $e) {
          $error_message = $e-&gt;getMessage();
          display_db_error($error_message);
      }
  }
  </code></pre>
  
  <p>It's not too important but at least it gives you some context.</p>
  
  <pre><code>    SELECT *
      FROM (
           SELECT b.*, ROW_NUMBER() OVER (PARTITION BY b.blogType ORDER BY b.blogID DESC) as rn
           FROM blogs b
           ) x
      WHERE x.rn &lt;= 3
      ORDER BY x.blogType, x.blogID DESC';
  </code></pre>
  
  <p>So I've done my best to implement the solution but I'm getting some errors. I'm not sure if I should start a new post for this one or not but the code above is what I used and this is the error I'm getting:</p>
  
  <pre><code>You have an error in your SQL syntax; it seems the error is around: '( PARTITION BY b.blogType ORDER BY b.dateWritten DESC ' at line 7
  </code></pre>