0

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

3 Answers 3

1

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'));
Sign up to request clarification or add additional context in comments.

Comments

0

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();

1 Comment

P_ID, C_ID, SN, R_MAC, M_MAC, U_MAC, E_MAC, C_MAC, touched_at
0

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();

3 Comments

can you show me table's dummy record because i used this query on my end and its give correct result
try this query --- DB::table('table')->where('touched_at', 'like', "$date%")->groupBy('C_ID')->count();
I need the distinct command otherwise i get all C_ID grouped seperated but i need them combined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.