我如何计算帖子的数量?
Posted
技术标签:
【中文标题】我如何计算帖子的数量?【英文标题】:How can I count the number of posts? 【发布时间】:2017-08-19 04:22:00 【问题描述】:这是我的表结构:
-- reputations
+----+-------------+---------+-------+------------+------------+
| id | post_id | user_id | score | reputation | date_time |
+----+-------------+---------+-------+------------+------------+ -- Suppose:
| 1 | 1 | 1 | 1 | 5 | 1500489844 | -- out of last week
| 2 | 4 | 3 | -1 | -2 | 1500499815 | -- out of last week
| 3 | 2 | 3 | 1 | 5 | 1500584821 |
| 4 | 3 | 1 | 1 | 5 | 1501389166 |
| 5 | 2 | 4 | 1 | 5 | 1501399142 |
| 6 | 2 | 1 | -1 | -2 | 1501399142 |
| 7 | 4 | 1 | 0 | 15 | 1501481186 |
| 8 | 5 | 1 | 1 | 5 | 1501481297 |
+----+-------------+---------+-------+------------+------------+
-- Note: the last row came from an accepted-answer, that's why its score is 0
-- post_tag
+---------+--------+
| post_id | tag_id |
+---------+--------+
| 1 | 2 |
| 1 | 4 |
| 2 | 2 |
| 3 | 1 |
| 3 | 4 |
| 4 | 3 |
| 5 | 1 |
+---------+--------+
-- tags
+----+--------+
| id | name |
+----+--------+
| 1 | php |
| 2 | html |
| 3 | css |
| 4 | mysql |
+----+--------+
这是我的查询:
SELECT
t.tag, sum(r.reputation) AS tag_reputation, sum(r.score) AS tag_score
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;
这是当前的结果:
tag | tag_reputation | tag_score
----: | :------------- | :---------
css | 15 | 0
php | 10 | 2
mysql | 5 | 1
html | -2 | -1
如您所见,结果是一个标签列表,其中包含由tag_reputation
排序的特定用户的声誉和分数。 Fiddle
现在我还想计算每个标签的帖子数。所以这是预期的结果:
tag | tag_reputation | tag_score | post_num
----: | :------------- | :--------- | :-------
css | 15 | 0 | 1
php | 10 | 2 | 2
mysql | 5 | 1 | 1
html | -2 | -1 | 1
我该怎么做?我想我必须处理post_id
列和GROUP BY
子句。但我不知道具体是怎样的。
【问题讨论】:
【参考方案1】:您是否尝试将帖子 ID 添加到查询中?
我在您的select
语句中添加了count(r.post_id) as post_num
,它是gave the expected results。
要删除重复项,请使用distinct
。你想要关于标签表的计数吗?试试
COUNT(distinct pt.post_id) AS post_count
;或
COUNT(distinct r.post_id) AS post_count
【讨论】:
【参考方案2】:我使用手机发布此内容,因此无法在您提供的小提琴上尝试。我修改您的 SQL 以使用 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;
编辑:我用 DISTINCT 添加 COUNT。看看能不能解决。
【讨论】:
我已经在reputations
表中添加了一行,其中包含已经存在的相同帖子 ID。但可悲的是post_num
也增加了。 dbfiddle.uk/…
在您的新小提琴中,您在 reputations
表中添加了具有相同 post_id
(5) 和相同 user_id
(1) 的新记录,并且您希望不计算此记录?
显然不是。因为post_id(5)是重复的,算一次。
但是您想添加它的声誉吗?
是的。 . . . . .【参考方案3】:
您的预期结果的某些内容并不完全一致 - 例如,您的预期结果包括您在 where 子句中排除的日期之前的帖子,而原始查询的结果包括不是来自用户 id = 1 所以我总体上有点困惑,但是:
我想只是添加:
SELECT
t.tag, sum(r.reputation) AS tag_reputation, sum(r.score) AS tag_score, count(r.id) as post_num
应该使这项工作?
如果没有,您可以在您的选择语句中使用内联选择:
SELECT
t.tag, sum(r.reputation) AS tag_reputation, sum(r.score) AS tag_score,
max((select count(r2.id) from users u2
LEFT JOIN reputations r2 ON r2.user_id = u2.id AND r2.date_time > 1500584821
JOIN post_tag pt2 ON pt2.post_id = r2.post_id
JOIN tags t2 ON t2.id = pt2.tag_id
where t.tag = t2.tag)) 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;
【讨论】:
以上是关于我如何计算帖子的数量?的主要内容,如果未能解决你的问题,请参考以下文章