Home > Software engineering >  How do I get distinct values from the table along with the count of how many rows there are containi
How do I get distinct values from the table along with the count of how many rows there are containi

Time:05-01

IN SQL (and if possible how to write it in Laravel Eloquent), if there are 10 MySQL rows having value IN', 4 rows having value 'US', 7 rows having value 'FR'; I want the query result to be three rows:

  1. IN | 10
  2. US | 4
  3. FR | 7

CodePudding user response:

SELECT column_name, count (*) as c FROM table_name GROUP BY column_name ORDER BY c DESC

CodePudding user response:

Model::query()->groupBy('country_name')->select('counrty_name',DB::raw('COUNT(1)'))
  • Related