1

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

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

我从来没有使用过 MySQL 的JOINorUNION函数,所以我不确定这是否可能。

如果有帮助,这是我的两个查询:

$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 页面中。

4

2 回答 2

2

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

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

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

更新

//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));
    }
}
于 2013-01-25T01:16:04.003 回答
0

您可以执行 aLEFT JOIN来获取博客文章和评论(如果没有评论,则只获取博客文章):

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

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

于 2013-01-25T01:16:57.893 回答