postgresql Rails 4 查询唯一的单个属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18050282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:03:20  来源:igfitidea点击:

Rails 4 query unique by single attribute

ruby-on-railspostgresqlactiverecordarel

提问by goddamnyouryan

So this is more of an arel question than anything but here's what I am trying to do.

所以这更像是一个问题而不是任何问题,但这就是我想要做的。

I have three objects lets say, called Items

我有三个对象可以说,称为 Items

<Item id: 1, name: 'Book'>
<Item id: 2, name: 'Car'>
<Item id: 3, name: 'Book'>

I want to do a query that will just return only one of each unique "name" attributes.

我想做一个查询,它只会返回每个唯一的“名称”属性中的一个。

Something like Item.select('distinct(name), items.*')

就像是 Item.select('distinct(name), items.*')

This doesn't work though, it still returns all three items.

但这不起作用,它仍然返回所有三个项目。

How can I form this query so that it only returns:

我怎样才能形成这个查询,以便它只返回:

<Item id: 1, name: 'Book'>
<Item id: 2, name: 'Car'>

回答by BananaNeil

If you want to get the entire model back, but still maintain uniqueness, you can use this:

如果你想恢复整个模型,但仍然保持唯一性,你可以使用这个:

Item.select('distinct on (name) *')

I've only tested this with a Postgres database. It may or may not work with mysql.

我只用 Postgres 数据库对此进行了测试。它可能与 mysql 一起工作,也可能不工作。

回答by vee

Please try this:

请试试这个:

Item.select('distinct name')

回答by Frexuz

If you only need an array with the names, without the ID, you can do:

如果你只需要一个有名字的数组,没有 ID,你可以这样做:

Item.pluck(:name).uniq

SQL query result:

SQL查询结果:

#=> SELECT `items`.`name` FROM `items`

** edit **

** 编辑 **

The uniqis ran on the array of records. Be careful if you expect a lot of records. SQL Distinct is much faster.

uniq是记录阵列上跑。如果您期望有很多记录,请小心。SQL Distinct 快得多。

If so, use vee's answer above, with a map:

如果是这样,请使用上面 vee 的答案,并带有map

Item.select('distinct name').map(:name)

Item.select('distinct name').map(:name)

回答by Oguzhan Ozdemir

I can't comment on posts yet, so, putting this as another answer for the question.

我还不能对帖子发表评论,所以,把它作为这个问题的另一个答案。

In case of someone still searches for this, @BananaNeil's answeris correct. However, putting distinctin selectdidn't work for me (Rails 5.2.2). Separating these two did fix my problem.

如果有人仍在搜索此内容,@BananaNeil 的回答是正确的。然而,将distinctselect没有对我来说有效(Rails的5.2.2)。将这两个分开确实解决了我的问题。

klass.where(
  # Your query or whatever
).distinct.select('on (attribute) *')

回答by TalkativeTree

In Rails 4 try Items.all.to_a.uniq { |item| item.name }

在 Rails 4 中尝试 Items.all.to_a.uniq { |item| item.name }

In Rails 3 you should be able to just do Items.uniq_by { |item| item.name }

在 Rails 3 中,你应该能够做到 Items.uniq_by { |item| item.name }

When you call uniqon an array, you can pass it a block to dictate how to determine uniqueness. In Rails 3 you used to be able to use uniq_by, but it became deprecated in Rails 4. So one method I found is to just convert the ActiveRecord Relation to an array and call uniqon that.

当您调用uniq一个数组时,您可以向它传递一个块来指示如何确定唯一性。在 Rails 3 中,您曾经可以使用uniq_by,但它在 Rails 4 中已被弃用。所以我发现的一种方法是将 ActiveRecord Relation 转换为数组并调用uniq它。

回答by fangxing

For Mysql, your can use groupwith ONLY_FULL_GROUP_BYdisabled,

对于 Mysql,您可以group在 禁用ONLY_FULL_GROUP_BY的情况下使用,

Item.group(:name).to_sql
=> "SELECT `items`.* FROM `items`  GROUP BY name"

See Is there ANY_VALUE capability for mysql 5.6?

请参阅mysql 5.6 是否有 ANY_VALUE 功能?