B
B
BonBon Slick2017-01-08 18:48:08
MySQL
BonBon Slick, 2017-01-08 18:48:08

Group group by 'col' as 'value'?

$allVotes = DB::table('votes')
    ->select('voted_for', DB::raw('count(*) as value'))
    ->groupBy('voted_for', DB::raw('voted_for as value2'))
    ->get();

Group as value2 .
In the first case, it worked well, but the second one is mistaken somewhere.
We need to group by the voted_for column and output it as value2 .

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BonBon Slick, 2017-01-09
@BonBonSlick

$allVotes = DB::table('votes')
    ->select('voted_for', DB::raw('count(*) as value'))
    ->groupBy('voted_for')
    ->get();

    foreach ($allVotes as $key => $value) {
      $value->label = $value->voted_for;
      $value->highlight = 'rgba(255, 255, 255, .1)';
      unset($value->voted_for);
    }

A
akzhan, 2017-01-09
@akzhan

in the group by grouping condition, no selection takes place there.
you need
I don't know how it is written in your ORM. I suspect how

$allVotes = DB::table('votes')
    ->select('voted_for', DB::raw('count(*) as value2'))
    ->groupBy('voted_for')
    ->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question