0

working with laravel 5.6 and mysql. I have following table name as projects

id  name  type  
1   tuna  bio
2   nhye  IT
3   hyuj  bio
4   tour  IT
5   ghyt  commerce

I need count type column values which are unique values. as an example in above table I need count values 3 because there are three different values like bio, IT and commerce. how can I do this?

Muru
  • 217
  • 5
  • 20

3 Answers3

2

Using Query builder

DB::table('projects')
        ->distinct()
        ->count('type');
Prashant Deshmukh.....
  • 2,244
  • 1
  • 9
  • 11
1

you can use this SQL query :

SELECT COUNT(DISTINCT type) as 'number total' FROM projects

The column 'number total' will be the number of differents value in your column 'type'

Fizik26
  • 753
  • 2
  • 10
  • 25
0

you can either use 2 ways in order to get the correct answer.

select Count(distinct type) from Projects

OR

select type, count(1) from Projects group by type;

I have used the table name as laravel instead Project

enter image description here

$data = DB::table("projects")

    ->select(DB::raw("COUNT(1) as count_row"))

    ->groupBy(DB::raw("(type)"))

    ->get();

print_r($data);

vikram sahu
  • 161
  • 12