When i use User.count(:all, :group => "name"), i got multi rows, but it's not i want, what i want is the count of the rows, how can I do it?
+2
A:
User.count
will give you the total number of users and translates to the following SQL:SELECT count(*) AS count_all FROM "users"
User.count(:all, :group => 'name')
will give you the list of unique names, along with their counts, and translates to this SQL:SELECT count(*) AS count_all, name AS name FROM "users" GROUP BY name
I suspect you want option 1 above, but I'm not clear on what exactly you want/need.
hgimenez
2009-10-30 13:29:15
thanks hgimenez, what i want is select count(*) as count_all) from (select name as name from users group by name)
2009-10-31 15:48:13
In that case, what Sikachu's answer seems more appropriate - you want the number of unique names, and that'll do.
hgimenez
2009-10-31 17:24:24
+2
A:
Probably you want to count the distinct name of the user?
User.count(:name, :distinct => true)
would return 3 if you have user with name John, John, Jane, Joey (for example) in the database.
________
| name |
|--------|
| John |
| John |
| Jane |
| Joey |
|________|
Sikachu
2009-10-31 15:25:34