我正在尝试使用 Active Record 计算具有不同列的行数。
以下 SQL 有效并给出了正确的结果:
SELECT COUNT(DISTINCT(user_id)) FROM attempts WHERE score = 100 AND problem_id = 1
但是此代码会引发 ActiveRecord::StatementInvalid
错误:
attempts.where(:score => 100).count(:distinct => :user_id)
最佳答案
尝试:
attempts.count('user_id', :conditions => "score = 100", :distinct => true)
更多信息:http://ar.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html
关于mysql - 计算具有不同列的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5754236/