我的要求是按顺序获取不同的记录
User.joins('INNER JOIN report_posts ON posts.id = report_posts.post_id').select('DISTINCT ON (report_posts.post_id) posts.id as report_posts.id as reported_id, report_posts.reported_at').order('report_posts.reported_at desc')
我知道postgresql
无法做到这一点,我已经阅读了Postgresql DISTINCT ON with different ORDER BY
我想要解决方案,我该如何做到这一点,它的替代方式?
答案 0 :(得分:4)
您需要在订单中包含DISTINCT列:
.order('report_posts.post_id, report_posts.reported_at desc')