tags:

views:

73

answers:

6

Hi,

Is it possible to print out (PHP) all my blog posts + associated comments via one sql query?

If so, how?

I was thinking in this direction:

SELECT p.post_id, p.title, c.comment_body
FROM posts p
LEFT JOIN comments c
ON c.parent_id = p.post_id

But this didn't work out as I expected

A: 

The easiest way I can think of is to iterate through all the rows returned and group them into an associative array where keys are the post IDs. Then you can iterate through that associative array and print the comments for each post, taking the post title from the first row in the group.

Ignas R
A: 

When getting data just use the field name like: $result = mysql_query("SELECT p.post_id, p.title, c.comment_body FROM posts p LEFT JOIN comments c ON c.parent_id = p.post_id");

while($row = mysql_fetch_array($result))
  {
  echo $row['title'] . " " . $row['comment_body'];
  echo "<br />";
  }

From: http://www.tizag.com/mysqlTutorial/mysqljoins.php

Scott
But what if there are multiple comments?
Bundy
It should be $row['comment_body'][1] for the next comment
Scott
A: 

If you're using MySQL you could use the GROUP_CONCAT function:

SELECT p.post_id, p.title, GROUP_CONCAT(c.comment_body)
FROM posts
LEFT JOIN comments c ON c.parent_id = p.post_id
GROUP BY p.post_id
CodeAddict
A: 

For MySQL:

SELECT p.post_id, p.title, GROUP_CONCAT(c.comment_body), count(*) as coment_cnt
FROM
    posts p
        LEFT JOIN comments c ON (p.post_id = c.parent_id)
GROUP BY
    p.post_id
Valery Victorovsky
A: 

"But this didn't work out as I expected "

...but you don't say what you did expect.

Assuming that the the implied schema in your query is correct, then its a no brainer to only show the posts once:

$lastpostid=false;
while ($r=mysql_fetch_assoc($result)) {
  if ($r['post_id']!=$lastpost) {
     print "Post: " . $r['title'] . "<br />\n";
     $comment_id=1;
     $lastpost=$r['post_id'];
  }
  print "Comment # $comment_id : " . $r['comment_body'] . "<br />\n";
  $comment_id++;
}

But as I said this implies that your query is correct (i.e. that comments are not hierarchical).

C.

symcbean
+2  A: 

Using one SQL query is not very convenient, since you have 1 post and multiple comments.
Having the post details added to each comment (in a combined query) is a waste of resources.

It is much more convenient to get the post details and use post_id of the post to find the comments belonging to the post.

Veger
+1. This is a situation that calls for two queries -- it's a one-to-many relationship of posts to comments. Only if it was a one-to-one relationship, would it be advisable to try and get data from both tables with a join.
Frank Farmer
Agreed. This is definitely a situation where it just doesn't make sense to use one query.
inxilpro
I need this because I want to create an XML file containing all data (all posts + 10 most recent comments/post). I don't know what the best solution is when you got 100 posts. That means you have to execute 100 sql queries?
Bundy
No 2 queries are required: one to grab the post details and one to grab the comment details. When only 10 comments are required use the 'LIMIT' and 'ORDER BY' SQL statements. The results of the comment query can be read using a loop and mysql_fetch_array() or something similar (depending on your needs)
Veger