SELECT *
FROM topic t post_actions pa INNER JOIN users u ON u.id = pa.user_id
WHERE post_id = 1
AND post_action_type_id = 2
AND u.created_at < t.created_at-2
另外,我需要帮助将主题 t 添加为另一个内部连接,但我已经解决了: FROM post_actions pa INNER JOIN users u ON u.id = pa.user_id INNER JOIN topics t on t.id = t.id
t.id 显示为数字而不是预览,有什么办法可以解决这个问题吗?
所以我想我已经用这个解决了。这产生了1000多个结果,所以我尝试添加 AND t.id = post.topic_id
这把列表缩短到16个结果,但仍然有4个相同的东西。不知道该如何解决。
SELECT
t.id as topic_id,
pa.post_id,
pa.user_id,
pa.created_at,
u.created_at
FROM post_actions pa INNER JOIN users u ON u.id = pa.user_id INNER JOIN topics t on t.id = t.id INNER JOIN posts p on p.topic_id = p.topic_id
WHERE post_id = 15000
AND t.id = 7000
AND post_action_type_id = 2
AND u.created_at < t.created_at - INTERVAL '2 DAY'
编辑:将 INNER JOIN posts p on p.topic_id = p.topic_id
更改为 INNER JOIN posts p on p.topic_id = t.id
这将其缩减到16个结果,与之前相同。
-- [params]
-- int :post_id
-- date :date
SELECT pa.user_id,
pa.created_at AS reltime$time
FROM post_actions pa
JOIN users u ON u.id = pa.user_id
WHERE pa.post_id = :post_id
AND post_action_type_id = 2
AND u.created_at::date > :date
ORDER BY pa.created_at DESC