LoginSignup
0

More than 5 years have passed since last update.

mysql/Railsでユーザーの年代ごとに集計する

Last updated at Posted at 2016-11-18

Userモデルのbirthdayカラムに'2010-01-08'というふうに年代が入っている場合

User.select("count(user.id) as count, truncate( timestampdiff(year,birthday,curdate())/10,0) as age").group(:age)

とすると、10代、20代...のように年代ごとに集計できます。

timestampdiffについてはこちら
https://dev.mysql.com/doc/refman/5.6/ja/date-calculations.html

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0