如何使用 arel/关系代数获取不同的值

发布于 2024-09-07 13:27:58 字数 157 浏览 10 评论 0原文

我正在尽我最大的努力来思考 arel 及其背后的关系代数,但如何表示 SELECT DISTINCT 始终让我无法理解。任何人都可以解释如何arel:

SELECT DISTINCT title FROM posts; 

非常感谢!

I'm doing my best to bend my brain around arel and the relational algebra behind it, but how to represent a SELECT DISTINCT is consistently eluding my comprehension. Can anyone explain how to arel:

SELECT DISTINCT title FROM posts; 

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

つ低調成傷 2024-09-14 13:27:58

使用纯 Arel(不是 Rails/ActiveRecord)有一个“不同”方法:

Arel::VERSION # => '3.0.2'
posts = Arel::Table.new(:posts)
posts.project(posts[:title])
posts.distinct
posts.to_sql # => 'SELECT DISTINCT "posts"."title" FROM "posts"'

奇怪的是,根据其他 Arel 方法,“不同”方法是不可链接的。

Using pure Arel (not Rails/ActiveRecord) there is a "distinct" method:

Arel::VERSION # => '3.0.2'
posts = Arel::Table.new(:posts)
posts.project(posts[:title])
posts.distinct
posts.to_sql # => 'SELECT DISTINCT "posts"."title" FROM "posts"'

Curiously, the "distinct" method is not chainable, per the other Arel methods.

墨离汐 2024-09-14 13:27:58

Arel 的方法是:

t = Arel::Table.new(:foo)
count_distinct = t[:field].count(true)
count_distinct.to_sql # => "COUNT(DISTINCT `foo`.`field`)"

The Arel way to do it is:

t = Arel::Table.new(:foo)
count_distinct = t[:field].count(true)
count_distinct.to_sql # => "COUNT(DISTINCT `foo`.`field`)"
做个ˇ局外人 2024-09-14 13:27:58

前面的答案是Rails 方式,不是吗?不是阿雷尔的方式。

这适用于 1.x:

posts = Table(:posts)
posts.project(Arel::Distinct.new(posts[:title]))

我会我猜还有另一种“更正确”的方法可以通过 API 来做到这一点,但我还没有弄清楚。

The previous answer is the Rails way, no? Not the Arel way.

This works for 1.x:

posts = Table(:posts)
posts.project(Arel::Distinct.new(posts[:title]))

I'd guess there's another "more correct" way to do this via the API but I haven't figured that out yet.

北音执念 2024-09-14 13:27:58

如果您使用范围来执行此操作:

  scope :recent, lambda {|count|
    select("DISTINCT posts.*").
    joins(:whatever).
    limit(count).
    order("posts.updated_at DESC")
  }

If you are doing this using a scope:

  scope :recent, lambda {|count|
    select("DISTINCT posts.*").
    joins(:whatever).
    limit(count).
    order("posts.updated_at DESC")
  }
暮年慕年 2024-09-14 13:27:58

Post.select('DISTINCT title')

更新 1:

在发帖时,Arel 中尚不支持此功能。如今,ActiveRecord::QueryMethods 有 uniq 方法 (http://apidock.com/rails/ActiveRecord/QueryMethods/uniq" rel="nofollow noreferrer">http:// /apidock.com/rails/ActiveRecord/QueryMethods/uniq),所以您需要:

Post.select(:title).uniq

更新 2:
看起来 Arel 现在支持这种行为。 @maerics 有正确答案。如果这不是被接受的答案,我会删除它。

Post.select('DISTINCT title')

Update 1:

At the time of the post, this was not available in Arel. These days, ActiveRecord::QueryMethods has the uniq method (http://apidock.com/rails/ActiveRecord/QueryMethods/uniq), so you'd want:

Post.select(:title).uniq

Update 2:
Looks like Arel now supports this behavior. @maerics has the correct answer. I'd delete this if it wasn't the accepted answer.

天暗了我发光 2024-09-14 13:27:58

由于 AREL 在其操作中始终使用 SET,因此重复的行结果将被自动删除。只需使用正常的 Project (Phi) 操作即可。

Since AREL always uses SET in it's operation, duplicate row results will be deleted automatically. Just use a normal Project (Phi) operation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文