错误:SELECT DISTINCT ON表达式必须与初始ORDER BY表达式匹配

时间:2016-05-25 09:46:27

标签: ruby-on-rails ruby postgresql

我的要求是按顺序获取不同的记录

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

我想要解决方案,我该如何做到这一点,它的替代方式?

1 个答案:

答案 0 :(得分:4)

您需要在订单中包含DISTINCT列:

.order('report_posts.post_id, report_posts.reported_at desc')