S
S
Sergey Suntsev2016-04-26 09:48:18
Yii
Sergey Suntsev, 2016-04-26 09:48:18

How to translate sql query from group by to active record?

There is a working request

SELECT  `u_id` , COUNT(  `u_id` ) AS  `rating` 
FROM  `u_h` 
GROUP BY  `u_id`

There is a desire to transfer it to active record, but I do not understand how to rename a column in it.
I try like this but it gives me an error
$query = U_h::find(); 
            $u_rating = $query
            ->select(['u_id'], [count('u_id') as 'rating'])
            ->from('u_h')
            ->groupBy('u_id')
            ->all();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-04-26
@GreyCrew

$query = U_h::find()->select(['u_id', 'rating' => 'COUNT(u_id)'])->from('u__H')->groupBy('u_id')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question