我有以下查询:Rails 3,ActiveRecord,PostgreSQL - “.uniq”命令不起作用?
Article.joins(:themes => [:users]).where(["articles.user_id != ?", current_user.id]).order("Random()").limit(15).uniq
,并给我的错误
PG::Error: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
LINE 1: ...s"."user_id" WHERE (articles.user_id != 1) ORDER BY Random() L...
当我更新原始查询到
Article.joins(:themes => [:users]).where(["articles.user_id != ?", current_user.id]).order("Random()").limit(15)#.uniq
所以错误消失...在MySQL .uniq的作品中,在PostgreSQL中没有。存在任何替代方案?
你确定用'uniq'查询返回不同的结果吗?请为每个变体显示实际的SQL查询(使用'sql'方法)。 – taro 2012-03-18 14:01:39
我确定。当我在MySQL数据库中使用这个查询时,如果没有'.uniq',它也会返回同一行,但'.uniq'总是唯一的行。在PostgreSQL中,如果我使用'.uniq',我得到了上面指定的错误,如果我不使用'.uniq',所以错误消失了,但是我也得到了来自数据库的相同行。 – user984621 2012-03-18 14:47:54