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

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

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

-- 昨日获赞最多的 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 篇帖子查询

-- 昨日浏览量最多的 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 机器人生成!)

1 个赞