简体   繁体   English

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

[英]How to find comment count and review count of a given Article in SQL..?

I have 2 tables : 我有2张桌子:

  • review (including content_type_id , object_id ) 评论 (包括content_type_idobject_id
  • comments (including content_type_id , object_id ) 注释 (包括content_type_idobject_id

The combination of " content_type_id and object_id " uniquely defines an entity (like article etc) content_type_id和object_id ”的组合唯一定义了一个实体(例如商品等)


Now i want to write a single SQL Query to : 现在我想编写一个SQL查询到:

"list all the articles with their 'review_count' and 'comment_count' ". “列出所有带有'review_count'和'comment_count'的文章”。

I mean in final result , what i want is : 我的意思是最终结果,我想要的是:

content_type_id | content_type_id | object_id | object_id | review_count | review_count | comment_count comment_count

How can i acheive this..?? 我该如何做到.. ??

(Edited after question changed and I found a data-bug) (问题更改后编辑我发现了一个数据错误)

Try this one: 试试这个:

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

It does not use a central table (formerly called article ). 它不使用中央表(以前称为article )。 This means, that items without any comment and without any review will not show up in the result. 这意味着没有任何评论和任何评论的项目将不会显示在结果中。 Items where at least one comment or one review is there will show up. 将显示至少有一条评论或一条评论的项目。

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

Off the top of my head but I think it would work: 从我的头顶上移开,但我认为它会起作用:

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

This would go only for articles with reviews and/or comments. 仅适用于带有评论和/或评论的文章。 To get ones without use a left join. 要获得一个不使用左联接。

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

I would begin by keeping a consistent naming convention on my tables and keep them all named in the singular 我将从在表上保持一致的命名约定开始,并将它们全部以单数命名。

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

Here's some test data to play with: 以下是一些测试数据:

-- 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

After you significantly changed your question, I believe this query will provide you an answer: 在您显着地更改了问题之后,我相信此查询将为您提供答案:

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

And here is some test data to work with: 这是一些可以使用的测试数据:

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

try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM