【问题标题】:Joining a 'blog' table and 'comments' table in MySQL在 MySQL 中加入“博客”表和“评论”表
【发布时间】:2013-01-08 22:38:57
【问题描述】:

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

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

我从未使用过 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 页面中。

【问题讨论】:

    标签: php mysql join mysqli union


    【解决方案1】:

    在不知道表结构的情况下,您可以尝试以下操作:

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

    如果您想返回没有 cmets 的博客,您可能需要使用 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));
        }
    }
    

    【讨论】:

    • 好的,那么我将如何使用它,并且仍然将博客作者、图片、发布日期、id 和内容放入变量中,同时将评论 id、作者、内容和 blog_id 也放入变量中?通过 2 个查询,我正在使用 mysqli_fetch_assoc()
    • 不知道你想做什么或上下文,我真的猜不出你。
    • 我用更多代码和进一步解释更新了我的问题。如果可以,请提供帮助。
    • @TyBailey 我已经更新了你的代码。看看它是否有效。我真的不认为将这两个查询结合起来是明智的,因为它们并不完全相关。
    • 不,很抱歉它不起作用。谢谢你的尝试。我会将查询分开。
    【解决方案2】:

    您可以通过LEFT JOIN 获取博文和 cmets(如果没有 cmets,则仅获取博文):

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

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

    【讨论】:

    • 你认为我坚持两个查询会更好吗?
    猜你喜欢
    • 1970-01-01
    • 2011-03-18
    • 2013-04-16
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多