0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails groupでカラム別にcountする

Last updated at Posted at 2019-11-07

countする時groupでカラム別にcountする

#Userモデル
User(id: integer, email: string, status: integer)

# User.statuses
{"init"=>0, "using"=>1}

User.all.count
   (233.9ms)  SELECT COUNT(*) FROM `users`
 => 10 

User.all.group(:status).count
   (135.6ms)  SELECT COUNT(*) AS count_all, `users`.`status` AS users_status FROM `users` GROUP BY `users`.`status`
 => {"init"=>4 "using"=>6} 

0
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?