一个查询中的文章和评论

时间:2014-03-27 13:37:06

标签: php mysql sql nested-queries

让我们说,我有两张桌子:

文章(articleId,articleDetail)

评论(commentId,commentDetail,commentArticle)

我将文章评论表与 commentArticle = articleId

相关联

让我们说,我收到了有关此查询的文章;

SELECT * FROM articles ORDER BY articleId DESC

结果是;

Array(
    [0] => stdClass Object
        (
            [articleId] => 3
            [articleDetail] => The details of article 3
        )
    [1] => stdClass Object
        (
            [articleId] => 2
            [articleDetail] => The details of article 2
        )
    [2] => stdClass Object
        (
            [articleId] => 1
            [articleDetail] => The details of article 1
        )
    ...
)

你知道,像往常一样;)

我想只用一个查询来获取文章和评论。我试过了;

SELECT 
    articles.*,
    comments.*
FROM 
    articles
LEFT JOIN
    comments ON comments.commentArticle = articles.articleId
ORDER BY
    articles.articleId
DESC

否!它没有用。我想用一个查询来获取评论和文章,就像这样;

Array(
    [0] => stdClass Object
        (
            [articleId] => 3
            [articleDetail] => The details of article 3
            [articleComments] => Array(
                                [0] => stdClass Object
                                       (
                                            [commentId] => 1
                                            [commentDetail] => Details of comment 1
                                            [commentArticle] => 3
                                       )
                                [1] => stdClass Object
                                       (
                                            [commentId] => 2
                                            [commentDetail] => Details of comment 2
                                            [commentArticle] => 3
                                       )
                                [2] => stdClass Object
                                       (
                                            [commentId] => 3
                                            [commentDetail] => Details of comment 3
                                            [commentArticle] => 3
                                       )
                          )
        )
    [1] => stdClass Object
        (
            [articleId] => 2
            [articleDetail] => The details of article 2
            [articleComments] => Array( )
        )
    [2] => stdClass Object
        (
            [articleId] => 1
            [articleDetail] => The details of article 1
            [articleComments] => Array(
                                [0] => stdClass Object
                                       (
                                            [commentId] => 4
                                            [commentDetail] => Details of comment 4
                                            [commentArticle] => 1
                                       )
                                [1] => stdClass Object
                                       (
                                            [commentId] => 1
                                            [commentDetail] => Details of comment 5
                                            [commentArticle] => 3
                                       )
                          )
        )
    ...
)

有没有办法用一个查询来做到这一点?甚至可能重新排序评论?

谢谢。

3 个答案:

答案 0 :(得分:1)

您可以选择此选项的唯一方法是始终在每条评论中都包含文章信息。 没有办法做你想要的。您将始终获得具有相同数量的列的行

答案 1 :(得分:0)

使用Postgres,你可以使用数组来实现:

SELECT 
       articles.*,
       array(select comments.single_column 
                    -- or (comments.column1 || '|' || commnets.column2) 
                    -- where '|' is any column separator you wanna use
               from comments 
              where comments.commentArticle = articles.articleId
            ) as comments_array
FROM 
       articles
ORDER BY
       articles.articleId DESC

不确定其他数据库(如SQLite / Oracle /等)是否支持这种实现。

答案 2 :(得分:0)

我能想到的最接近的是:

select
  a.articleId,
  articleDetail,
  concat('[', group_concat(
    concat('["commentId"=>"', commentId,
      '", commentDetail=>"', commentDetail,
      '", "commentArticle"=>"', commentArticle, '"]')), ']') comment_array 
from articles a
left join comments c on c.commentArticle = a.articleId
group by 1, 2
order by a.articleId desc

这将为注释返回一个PHP数组数组的 string 版本作为单个值。由您的应用程序将其转换为实际数组的实际数组。