PG ::错误:SELECT DISTINCT,ORDER BY表达式必须出现在选择列表中 [英] PG::Error: SELECT DISTINCT, ORDER BY expressions must appear in select list

查看:1449
本文介绍了PG ::错误:SELECT DISTINCT,ORDER BY表达式必须出现在选择列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ActionView :: Template :: Error(PG :: Error:ERROR:对于SELECT DISTINCT,ORDER BY表达式必须出现在选择列表中

ActionView::Template::Error (PG::Error: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list

我正在创建一个事件网站,并试图按事件开始时间对呈现的rsvps进行排序. RSVPS有很多,因此我将它们分为不同的类别,但是在过去的几天里,在排序结果时遇到很多困难,而在PG上不会出现此错误.我已经看过以前有关该主题的一些问题,但仍然很迷茫.我怎样才能使它正常工作?非常感谢!

I'm creating an events website and I'm trying to sort the rendered rsvps by the start time of the event. There are a lot of RSVPS so I'm grouping them with distinct, but I've been having a lot of difficulty over the last few days on sorting the results without this error popping up on PG. I've looked at some of the previous questions on the topic and am still pretty lost. How can I get this to work? Thank you so much!

@rsvps = Rsvp.where(:voter_id => current_user.following.collect {|f| f["id"]}, :status => 'going').where("start_time > ? AND start_time < ?", Time.now, Time.now + 1.month).order("count_all desc").count(:group => :event_id).collect { |f| f[0] }

<%= render :partial => 'rsvps/rsvp', :collection => Rsvp.where(:event_id => @rsvps).select("DISTINCT(event_id)").order('start_time asc') %>

推荐答案

仅在应用DISTINCT后 才能应用ORDER BY子句.由于DISTINCT操作仅考虑SELECT语句中的字段,因此在ORDER BY中只能使用这些字段.

The ORDER BY clause can only be applied after the DISTINCT has been applied. Since only the fields in the SELECT statement are taken into consideration for the DISTINCT operations, those are the only fields may be used in the ORDER BY.

从逻辑上讲,如果只需要一个event_id值的不同列表,则它们出现的顺序应该无关紧要.如果顺序很重要,则应将start_time添加到SELECT列表中,以便该顺序具有上下文.

Logically, if you just want a distinct list of event_id values, what order they occur in should be irrelevant. If order does matter, then you should add the start_time to the SELECT list so that there is context for the order.

此外,这两个SELECT子句不等效,因此请注意:

Also, these two SELECT clauses are NOT equivalent, so be careful:

SELECT DISTINCT(event_id, start_time) FROM ...

SELECT DISTINCT event_id, start_time FROM ...

第二个是您想要的表格.第一个将返回一系列记录,这些记录的数据表示为ROW构造(内部包含元组的单列).第二个将返回数据输出的普通列.它仅在单列情况下按预期方式工作,在这种情况下,由于ROW构造仅是单列,因此缩减了构造.

The second is the form you want. The first will return a series of records with the data represented as a ROW construct (a single column with a tuple inside). The second will return normal columns of data output. It only works as expected in the single-column case where the ROW construct is reduced down since it is only a single column.

这篇关于PG ::错误:SELECT DISTINCT,ORDER BY表达式必须出现在选择列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆