【问题标题】:Multiple distinct count for ActiveRecord resourceActiveRecord 资源的多个不同计数
【发布时间】:2015-01-21 19:22:59
【问题描述】:

我想按唯一属性对 attribute1 attribute2 计算记录。

所以对于表foo

-----------------------------
id | attribute1 | attribute2 |
-----------------------------
1  | x          | y          |
2  | x          | y          |
3  | x          | y1         |
4  | x1         | y1         |

我想得到 count = 3,所以计算记录:([x,y], [x,y1], [x1,y1])。 如何用 ActiveRecord 编写这个查询?

【问题讨论】:

    标签: mysql sql ruby-on-rails ruby


    【解决方案1】:

    这是一个简洁的方法:

    Foo.distinct.count("CONCAT(attribute1,attribute2)")
    

    【讨论】:

    • 再棒不过了! +1
    猜你喜欢
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    相关资源
    最近更新 更多