Timo
Timo

Reputation: 138

HOW TO: Laravel 5 - mySQL query

the situation:

I have one mySQL database and I want to get a dinstinct count of the column C_ID where the set timestamp is like the current day (e.g. 2015-08-14)

in phpMyAdmin it works fine:

SELECT COUNT(DISTINCT C_ID) FROM table WHERE touched_at LIKE '2015-08-14%'

the result is 32; but in Laravel 5 I get 320 because the distinct function does not work for me

$date = date('Y-m-d');

$one = DB::table('talbe')
        ->select()
        ->where('touched_at', 'like', "$date%")
        ->distinct('C_ID')
        ->count();

Can you please assist me with this problem?

Thanks in advance Timo

Upvotes: 0

Views: 394

Answers (3)

Cor Bosman
Cor Bosman

Reputation: 336

This may work. Tried it in one of my projects and that produces the right answer for me.

$one = DB::table('talbe')
         ->where('touched_at', 'like', "$date%")
         ->count(DB::raw('distinct C_ID'));

Upvotes: 1

Vipul
Vipul

Reputation: 941

You may also try this code

 $date = date('Y-m-d');
 $one = DB::table('table')
     ->select(DB::raw('count(Distinct C_ID) as C_ID'))
     ->where('touched_at', 'like', "$date%")
     ->get();

Upvotes: 0

aldrin27
aldrin27

Reputation: 3407

Try this if it works:

 $date = date('Y-m-d');

  $one = DB::table('talbe')
    ->select('touched_at')
    ->where('touched_at', 'like', "$date%")
    ->distinct('C_ID')
    ->count();

Upvotes: 0

Related Questions