[英]how to group by a number of ids in a postgres array
I am trying to select a row based upon a count of the nubmer of items in a Postgres array (represented as a text field in Postgres 9.4) and be able to select / group in Rails 4.2 我正在尝试根据Postgres数组(在Postgres 9.4中表示为文本字段)的nummer项的计数选择一行,并能够在Rails 4.2中选择/分组
I have the following tables: 我有以下表格:
queryable_id | liked_count | users_who_like_ids
--------------+-------------+--------------------
2376 | 3 | {1,80,78,101, 188}
18771 | 1 | {78,101, 123,125}
1790 | 1 | {78}
2257 | 1 | {78}
For example, I'd like to say select the and order by user_who_like_ids like 例如,我想说的是按user_who_like_ids like选择
select * from items group by count(users_who_like_ids) where users_who_like_ids include (78,123,125)
How would I make this query? 我将如何查询? Would this type of query be supported in rails? Rails是否会支持这种类型的查询?
here is the users_who_like_ids 这是users_who_like_ids
users_who_like_ids | text[] | default '{}'::text[]
It looks like this kinda works: 看起来有点像这样:
select queryable_id, liked_count, users_who_like_ids from queryables where users_who_like_ids @> ARRAY['1','80'];
but this requires both 1 and 80 但这需要1和80
I think, you are looking for array_length()
我认为,您正在寻找array_length()
select
*
from
items
where
users_who_like_ids @> ARRAY[78,123,125]
group by
array_length(users_who_like_ids)
Note, that the where
clause always belongs for the group by
clause. 注意, where
子句始终属于group by
子句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.