# 获取昨日点赞或者浏览量，前10的帖子

**URL:** https://meta.discourse.org/t/topic/382918
**Category:** Support
**Created:** [2025年九月17日 07:05 UTC](https://meta.discourse.org/t/topic/382918 "2025-09-17T07:05:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![zhang\_zhiyuan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zhang_zhiyuan/32/430883_2.png) [@zhang\_zhiyuan](https://meta.discourse.org/u/zhang_zhiyuan)
#### Post date: [2025年九月17日 07:05 UTC](https://meta.discourse.org/t/topic/382918/1 "2025-09-17T07:05:48Z")

</div>

我想获取昨日点赞或者浏览量，前10的帖子，有什么办法可以得到吗,似乎数据库存储的都是累计值，谢谢

---

<div class="post-metadata">

### Author: ![ondrej](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ondrej/32/198804_2.png) [@ondrej](https://meta.discourse.org/u/ondrej)
#### Post date: [2025年九月17日 07:56 UTC](https://meta.discourse.org/t/topic/382918/2 "2025-09-17T07:56:34Z")

</div>

嘿 🙂

你们的网站是否启用了 [Discourse Data Explorer](https://meta.discourse.org/t/discourse-data-explorer/32566) 插件？

---

<div class="post-metadata">

### Author: ![zhang\_zhiyuan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zhang_zhiyuan/32/430883_2.png) [@zhang\_zhiyuan](https://meta.discourse.org/u/zhang_zhiyuan)
#### Post date: [2025年九月19日 06:00 UTC](https://meta.discourse.org/t/topic/382918/3 "2025-09-19T06:00:45Z")

</div>

没有启用呢，是不是需要启用 才会记录更详细的数据

---

<div class="post-metadata">

### Author: ![nat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nat/32/235063_2.png) [@nat](https://meta.discourse.org/u/nat)
#### Post date: [2025年九月19日 18:43 UTC](https://meta.discourse.org/t/topic/382918/4 "2025-09-19T18:43:33Z")

</div>

您应该启用它，然后您将能够进行针对过去一天（昨天）的特定查询。

### 昨日获赞最多的 10 篇帖子查询

```sql
-- 昨日获赞最多的 10 篇帖子
WITH yesterday_actions AS (
  SELECT 
    post_id,
    COUNT(*) AS like_count
  FROM post_actions
  WHERE 
    created_at::date = CURRENT_DATE - 1
    AND post_action_type_id = 2 -- 点赞操作类型
  GROUP BY post_id
)

SELECT 
  p.id AS post_id,
  t.id AS topic_id,
  t.title AS topic_title,
  p.post_number,
  u.username AS author,
  ya.like_count AS likes_yesterday
FROM yesterday_actions ya
JOIN posts p ON p.id = ya.post_id
JOIN topics t ON t.id = p.topic_id
JOIN users u ON u.id = p.user_id
ORDER BY likes_yesterday DESC
LIMIT 10;

```

### 昨日浏览量最多的 10 篇帖子查询

```sql
-- 昨日浏览量最多的 10 篇帖子
WITH yesterday_topic_views AS (
  SELECT 
    topic_id,
    COUNT(*) AS view_count
  FROM topic_views
  WHERE viewed_at::date = CURRENT_DATE - 1
  GROUP BY topic_id
)

SELECT 
  t.id AS topic_id,
  t.title,
  u.username AS creator,
  ytv.view_count AS views_yesterday
FROM yesterday_topic_views ytv
JOIN topics t ON t.id = ytv.topic_id
JOIN users u ON u.id = t.user_id
ORDER BY views_yesterday DESC
LIMIT 10;

```

（由 AI 机器人生成！）
