Reputation:
This is a pretty individual question. So I had a hard time finding a good answer on Stack. I'm looking to run a query that combines the information on two tables together.
So far this is what I got
$query = "SELECT blog_post.*, user.name, user.last_name, user.picture
FROM blog_post, user
INNER JOIN user ON blog_post.author_id = user.id
WHERE blog_post.author_id = ?
ORDER BY timestamp DESC LIMIT 10";
$Statement = $this->Database->prepare($query);
$Statement->execute(array($id));
$row = $Statement->fetchAll();
print_r($row); //Returns: Array( )
I need to display the user information along with the blog post. I could store the information when I save the post in the database but then it wouldn't update if the user updated their information. So I am trying to retrieve the user information from the user table at the same time as I retrieve the blog post.
How would I go about doing this with one sql query? I know I could easily do it by just calling another query.
Upvotes: 1
Views: 13322
Reputation: 79979
Remove , user
. It should be written this way:
SELECT blog_post.*, user.name, user.last_name, user.picture
FROM blog_post
INNER JOIN user ON blog_post.author_id = user.id
WHERE blog_post.author_id = ?
ORDER BY timestamp DESC
LIMIT 10
Upvotes: 3