加入MySQL中的“博客”表和“评论”表

时间:2013-01-25 01:14:15

标签: php mysql join mysqli union

我有两个单独的表,第一个是'博客',它显然拥有发布的博客。第二个表是“b_comments”,其中包含发布在博客上的所有评论。每个博客都有一个唯一的ID,相应地存储在每个评论中。

截至目前,我正在使用2个单独的查询来获取完整的博客文章以及随之而来的所有评论。有没有办法我可以JOIN将这两个表放在一起,以便在一个查询中获得整篇博文和所有评论?

我从来没有使用MySQL的JOINUNION函数,因此我不确定这是否可行。

以下是我的两个查询,如果这有助于任何:

$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")

编辑:这里有更多代码可以帮助您更好地了解我要做的事情:

//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));

使用这些变量,我将数据回显到HTML页面。

2 个答案:

答案 0 :(得分:2)

在不知道表格结构的情况下,您可以尝试以下内容:

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

如果您想要返回没有评论的博客,可能需要使用LEFT JOIN

<强>更新

//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));
    }
}

答案 1 :(得分:0)

您可以LEFT JOIN获取博文和评论(如果没有评论,可以只发布博客文章):

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

虽然在这种情况下,两个查询可能更有效,因为您将获取博客帖子以及每个评论行。