Ruby Gem Sequel - 如何仅显示计数值 > 1 的分组依据

Ruby Gem Sequel - how to show only group by with count value > 1

DB[:items].group_and_count(:name).all

我得到一个包含出现次数的名字列表。

如果我只希望返回那些计数超过 e.g. 的名称(包括它们的计数),我需要做什么2? 在 SQL 我会做这样的事情:

SELECT name, count(name) FROM items GROUP BY name HAVING count(name) > 2

这个呢?

DB[:items].group('name').having('COUNT(name) > 2')

您只需添加 having 子句,在 Sequel 中可以通过多种方式完成。

# This uses virtual row as it is called in Sequel, with the { and }
DB[:items].group_and_count(:name).having{Sequel.function(:count, :name) > 2}.all

# or like this if you prefer
DB[:items].group_and_count(:name).having{COUNT(name) > 2}.all