加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

数据库 – Rails 3,ActiveRecord,PostgreSQL – “.uniq”命令不

发布时间:2020-12-12 16:30:44 所属栏目:MsSql教程 来源:网络整理
导读:我有以下查询: 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 listLINE 1: ...s"
我有以下查询:
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中没有.存在任何替代品?

解决方法

由于SELECT DISTINCT的错误状态,ORDER BY表达式必须出现在选择列表中.
因此,您必须明确选择您订购的子句.

这是一个例子,它类似于你的情况,但是泛化一点.

Article.select('articles.*,RANDOM()')
       .joins(:users)
       .where(:column => 'whatever')
       .order('Random()')
       .uniq
       .limit(15)

所以,使用.select()明确地包含你的ORDER BY子句(在这种情况下是RANDOM()).如上所示,为了让您的查询返回文章属性,您还必须明确选择它们.

我希望这有帮助;祝你好运

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读