Question

I have two separate tables, the first one is 'blogs' which obviously holds posted blogs. The second table is 'b_comments' which holds all the comments posted on the blogs. Each blog has a unique ID, which is stored with each comment accordingly.

As of right now I am using 2 separate queries to grab both the full blog post and all the comments that go with it. Is there a way I can JOIN these two tables together to get an entire blog post and all of the comments that go with it in one query?

I have never had to use the JOIN or UNION functions of MySQL so I am unsure if this is even possible.

Here are my two queries if that helps any:

$getBlog = $link->query("SELECT * FROM blogs WHERE blog_id = '" .$blogID. "'");

$getComments = $link->query("SELECT * FROM b_comments WHERE blog_id = '".$blogID."' ORDER BY date DESC")

EDIT: Here is more code to help you better understand what I am trying to do:

//get blog posts
$blogID = intval($_GET['id']);

$sql = $link->query("SELECT * FROM blogs WHERE blog_id = '" .$blogID. "'");

$row = mysqli_fetch_assoc($sql);

$blogTitle = $link->real_escape_string($row['blog_title']);
$pubDate = $link->real_escape_string($row['pub_date']);
$blogCat = $link->real_escape_string($row['category']);
$blogAuthor = $link->real_escape_string($row['author']);
$blogImage = $link->real_escape_string($row['image']);
$blogContent = $link->real_escape_string($row['content']);


//get comments
$getComments = $link->query("SELECT * FROM b_comments WHERE blog_id = '".$blogID."' ORDER BY date DESC");

if(!($getComments->num_rows > 0)) {

    echo "<div id='commentArea' style='display: none;'>";

    echo "</div>";
} else {                    
    echo "<div id='commentArea'>";  

    while($comRow = mysqli_fetch_assoc($getComments)) {
        $commentID = $link->real_escape_string($comRow['id']);
        $comAuthor = $link->real_escape_string($comRow['user_name']);
        $comDate = $link->real_escape_string($comRow['date']);
            $comContent = $link->real_escape_string($comRow['content']);
        $comDate = date("F jS, Y H:ia", strtotime($comDate));

With those variables I am echoing out the data into an HTML page.

No correct solution

OTHER TIPS

Without knowing the table structure you can try something like:

SELECT *
FROM blogs
JOIN b_comments ON blogs.blog_id = b_comments.blog_id

You may want to use a LEFT JOIN if you want to return blogs that do not have comments.

Update

//get blog posts
$blogID = intval($_GET['id']);

$blogTitle = null;
$pubDate = null
$blogCat = null;
$blogAuthor = null;
$blogImage = null;
$blogContent = null;

//get comments
$getComments = $link->query("SELECT a.blog_title, a.pub_date, a.category, a.author, a.image, a.content AS blog_content, b.id, b.user_name, b.date, b.content FROM blogs a JOIN b_comments b ON a.blog_id = b.blog_id WHERE a.blog_id = " . $blogID . " ORDER BY b.date DESC");

if(!($getComments->num_rows > 0)) {
    echo "<div id='commentArea' style='display: none;'>";

    echo "</div>";
} else {
    while($comRow = mysqli_fetch_assoc($getComments)) {
    if(empty($blogTitle)) $blogTitle = $link->real_escape_string($row['blog_title']);
    if(empty($pubDate)) $pubDate = $link->real_escape_string($row['pub_date']);
    if(empty($blogCat)) $blogCat = $link->real_escape_string($row['category']);
    if(empty($blogAuthor)) $blogAuthor = $link->real_escape_string($row['author']);
    if(empty($blogImage)) $blogImage = $link->real_escape_string($row['image']);
    if(empty($blogContent)) $blogContent = $link->real_escape_string($row['blog_content']);

    $commentID = $link->real_escape_string($comRow['id']);
    $comAuthor = $link->real_escape_string($comRow['user_name']);
    $comDate = $link->real_escape_string($comRow['date']);
    $comContent = $link->real_escape_string($comRow['content']);
    $comDate = date("F jS, Y H:ia", strtotime($comDate));
    }
}

You can do a LEFT JOIN to get the blog post and comments (or just the blog post if there are no comments):

$getBlogAndComments = $link->query("SELECT * FROM blogs b 
                              LEFT JOIN b_comments ON b.blog_id = c.blog_id 
                              WHERE b.blog_id = '" .$blogID. "'");

Although in this instance two queries may be more efficient, since you'll be fetching the blog post along with every comment row.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top