红宝石续集 - 如何仅显示计数值为 1 >的分组依据

  • 本文关键字:何仅 显示 红宝石 ruby sequel
  • 更新时间 :
  • 英文 :


使用

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

我得到了一个带有出现次数的名字列表。

如果我只想返回那些计数超过2的名字(包括他们的计数),我该怎么办?在SQL中,我会这样做:

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

您只需要添加have子句,这可以在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

这个怎么样?

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

最新更新