How can I count the number of posts?

后端 未结 3 1572
太阳男子
太阳男子 2021-01-29 03:37

Here is my table structure:

-- reputations
+----+-------------+---------+-------+------------+------------+
| id | post_id     | user_id | score | reputation | d         


        
3条回答
  •  忘掉有多难
    2021-01-29 04:20

    I post this using mobile phone so I cannot try it on the fiddle you gave. I modify your SQL to count number of post using COUNT.

    SELECT
    t.tag, 
    sum(r.reputation) AS tag_reputation, 
    sum(r.score) AS tag_score,
    COUNT(DISTINCT pt.post_id) AS post_num
    FROM
    users u
    LEFT JOIN reputations r 
        ON r.user_id = u.id 
            AND r.date_time > 1500584821
    JOIN post_tag pt ON pt.post_id = r.post_id
    JOIN tags t ON t.id = pt.tag_id
    WHERE u.id = 1 -- Specific user: Jack
    GROUP BY
    u.id, u.user_name, t.tag 
    ORDER BY
    u.id, tag_reputation DESC;
    

    Edit: I add COUNT with DISTINCT. See if it solve.

提交回复
热议问题