如何在SQL中查找给定文章的评论计数和审核次数?

时间:2012-02-09 05:10:59

标签: sql postgresql

我有两张桌子:

  • 审核(包括 content_type_id object_id
  • 评论(包括 content_type_id object_id

content_type_id和object_id ”的组合唯一地定义了一个实体(如文章等)


现在我想将一个 SQL查询写入:

“列出所有带有'review_count'和'comment_count'的文章。

我的意思是在最终结果中,我想要的是:

content_type_id | object_id | review_count |的 COMMENT_COUNT

我怎么能实现这个...... ??

6 个答案:

答案 0 :(得分:2)

(在问题改变之后编辑我发现了数据错误)

试试这个:

select 
    content_type_id, object_id,
    sum(review_count) review_count,
    sum(comment_count) comment_count
from
    (
        select 
            content_type_id, object_id, count(*) review_count 
        from jc_reviews 
        group by 1, 2
    ) as r_count
    full outer join 
    ( 
        select content_type_id, object_id, count(*) comment_count 
        from jc_comments 
        group by 1, 2
    ) as c_count
    using (content_type_id, object_id)
group by 1, 2

它不使用中央表(以前称为article)。这意味着,没有任何评论且没有任何评论的项目将不会显示在结果中。将显示至少一条评论或一条评论的项目。

答案 1 :(得分:1)

SELECT article_id, article_name,
(
    select count(review_id)
    from review 
    where article_id = a.articles
) review_count,
(
   select count(comments _id)
    from comments 
    where article_id = a.articles
) comment_count
FROM articles a

答案 2 :(得分:1)

脱离我的头脑,但我认为它会起作用:

SELECT
article_id,
article_name,
count(review_id) as review_count,
count(comment_id) as comment_count
FROM articles a, reviews b, comments c
WHERE
a.article_id = b.article_id OR
a.article_id = c.article_id

这仅适用于包含评论和/或评论的文章。为了得到一个没有使用左连接。

答案 3 :(得分:1)

select a.article_id,
    coalesce(rc.review_count, 0) as review_count,
    coalesce(cc.comment_count, 0) as comment_count
from articles a
left outer join (
    select article_id, count(*) as review_count
    from review
    group by article_id
) rc on a.article_id = rc.article_id
left outer join (
    select article_id, count(*) as comment_count
    from comments
    group by article_id
) cc on a.article_id = cc.article_id

答案 4 :(得分:1)

我首先要在我的表上保持一致的命名约定,并将它们全部命名为单数

SELECT article_id = a.article_id
       ,review_count = COUNT(DISTINCT(r.review_id))
       ,comment_count = COUNT(DISTINCT(c.comment_id))
  FROM article a
  LEFT OUTER JOIN review r
    ON a.article_id = r.article_id
  LEFT OUTER JOIN comment c
    ON a.article_id = c.article_id
 GROUP BY a.article_id

以下是一些测试数据:

-- Create tables
CREATE TABLE article(article_id INT)
CREATE TABLE review(review_id INT, article_id INT)
CREATE TABLE comment(comment_id INT, article_id INT)

-- Create some test data
INSERT INTO article SELECT 1
INSERT INTO article SELECT 2
INSERT INTO article SELECT 3

INSERT INTO review SELECT 10, 1
INSERT INTO review SELECT 20, 2
INSERT INTO review SELECT 30, 2
INSERT INTO review SELECT 40, 3
INSERT INTO review SELECT 50, 3
INSERT INTO review SELECT 60, 3

INSERT INTO comment SELECT 200, 1
INSERT INTO comment SELECT 300, 1
INSERT INTO comment SELECT 400, 2
INSERT INTO comment SELECT 500, 2
INSERT INTO comment SELECT 600, 3

在您明显更改了问题后,我相信此查询会为您提供答案:

SELECT u.content_type_id
       ,u.object_id
       ,review_count = SUM(u.review_count)
       ,comment_count = SUM(u.comment_count)
  FROM
    (
    SELECT content_type_id
           ,object_id
           ,review_count = COUNT(*)
           ,comment_count = 0
      FROM review
     GROUP BY content_type_id, object_id
     UNION
    SELECT content_type_id
           ,object_id
           ,review_count = 0
           ,comment_count = COUNT(*)
      FROM comment
     GROUP BY content_type_id, object_id
    ) u
GROUP BY u.content_type_id, u.object_id

以下是一些可以使用的测试数据:

CREATE TABLE review(review_id INT, content_type_id INT, object_id INT)
CREATE TABLE comment(comment_id INT, content_type_id INT, object_id INT)

INSERT INTO review SELECT 11, 10, 100
INSERT INTO review SELECT 12, 10, 100
INSERT INTO review SELECT 13, 20, 100
INSERT INTO review SELECT 13, 10, 200
INSERT INTO review SELECT 13, 30, 100
INSERT INTO comment SELECT 21, 10, 100
INSERT INTO comment SELECT 22, 20, 100
INSERT INTO comment SELECT 23, 20, 100
INSERT INTO comment SELECT 24, 20, 100
INSERT INTO comment SELECT 25, 10, 200
INSERT INTO comment SELECT 26, 10, 200

答案 5 :(得分:1)

试试这个:

SELECT  a.Article_ID,
        COUNT(b.Article_ID) as ReviewCount,
        COUNT(c.Article_ID) as CommentCount
FROM Articles a JOIN Review b ON
        a.Article_ID = b.Article_ID
    LEFT JOIN Comments c ON
        a.Article_ID = c.Article_ID
GROUP BY a.Article_ID