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!
I have done a bit of work already in googling how to limit the results. Shown below:
$query = 'SELECT * FROM blogs
ORDER BY dateWritten
ASC LIMIT 3';
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:
function get_Recent_Blogs() {
global $db;
$query = 'SELECT * FROM blogs
ORDER BY dateWritten
ASC LIMIT 3';
try {
$statement = $db->prepare($query);
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
$statement->closeCursor();
return $result;
} catch (PDOException $e) {
$error_message = $e->getMessage();
display_db_error($error_message);
}
}
It's not too important but at least it gives you some context.
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 <= 3
ORDER BY x.blogType, x.blogID DESC';
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:
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