【发布时间】:2021-10-15 10:36:09
【问题描述】:
有
DB[:items].group_and_count(:name).all
我得到一个包含出现次数的名称列表。
如果我只希望返回的名称(包括其数量)超过例如2? 在 SQL 中,我会这样做:
SELECT name, count(name) FROM items GROUP BY name HAVING count(name) > 2
【问题讨论】:
有
DB[:items].group_and_count(:name).all
我得到一个包含出现次数的名称列表。
如果我只希望返回的名称(包括其数量)超过例如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
【讨论】: