【发布时间】:2014-12-18 13:12:12
【问题描述】:
一个表有这样的结构和数据:
id | car | location | ...
-----------------------------
1 | BMW | US | ...
2 | Audi | US | ...
3 | Audi | US | ...
4 | Porsche | CA | ...
5 | Audi | US | ...
我正在以通常的方式加载和分组数据:
@cars = Cars.where('location = ?', location).group('car')
我需要在视图中显示一些分组数据 - 在这种情况下,我需要打印例如美国有多少数据;所以想要的输出是这样的:
BMW - 1 car
Audi - 3 cars
如何获取分组的行数?
【问题讨论】:
-
不知道
ruby,但在mysql中它会是select car,count(*) as total from table where location = 'US' group by car
标签: mysql ruby-on-rails ruby view group-by