我有一个 MySQL 表:
id int
a varchar
b varchar
c varchar
version int
active bool
我想通过 a、b 和 c 获取最大版本组,所以我有以下查询来执行此操作:
select a, b, c, max(version) as version from mytbl where active = 1 group by a, b, c
我在 Sinatra 中使用 Datamapper。上面的表模型名称是“mytbl”。 与上述查询等效的数据映射器是什么?
最佳答案
我明白了:)
mytbl.aggregate(:version.max, :active => 1, :fields => [:a, :b, :c], :unique => true, :order => nil)
或
mytbl.aggregate(:version.max, :conditions => [ 'active = ?', 1], :fields => [:a, :b, :c], :unique => true, :order => nil)
但是我找不到将 max(version) 别名为 version 的方法。它返回 max(version) 作为列名。这没什么大不了的;)
关于Ruby Datamapper 按最大分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12961864/