Answer the question
In order to leave comments, you need to log in
How to rewrite this SQL query in Query Builder?
There is a SQL query:
SELECT
iu.*, u.banned, u.foto,
(SELECT SUM(rating)/COUNT(*)
FROM `olit_insurance_reviews`
WHERE company_id = u.user_id and rating > 0 AND parent_id IS NULL) as rating,
(SELECT COUNT(*)
FROM `olit_insurance_reviews`
WHERE company_id = u.user_id AND parent_id IS NULL) as reviews_count
FROM `olit_insurance_users` as iu
INNER JOIN `olit_users` as u
ON u.user_id = iu.user_id
WHERE u.user_group = '6' ORDER BY u.banned DESC, iu.company_name ASC
->select('name')->from('table')->where(['like', 'yes']);
Answer the question
In order to leave comments, you need to log in
Look at the doc . Most of your questions are answered there. Here, for example, about nested selects:
$subQuery = (new Query())->select('COUNT(*)')->from('user');
// SELECT `id`, (SELECT COUNT(*) FROM `user`) AS `count` FROM `post`
$query = (new Query())->select(['id', 'count' => $subQuery])->from('post');
$sql = "SELECT
iu.*, u.banned, u.foto,
(SELECT SUM(rating)/COUNT(*)
FROM `olit_insurance_reviews`
WHERE company_id = u.user_id and rating > :rating AND parent_id IS NULL) as rating,
(SELECT COUNT(*)
FROM `olit_insurance_reviews`
WHERE company_id = u.user_id AND parent_id IS NULL) as reviews_count
FROM `olit_insurance_users` as iu
INNER JOIN `olit_users` as u
ON u.user_id = iu.user_id
WHERE u.user_group = :userGroup ORDER BY u.banned DESC, iu.company_name ASC";
$cmd = Yii::$app->db->createCommand($sql, [':userGroup' => 6, ':rating' => 0]);
$items = $cmd->queryAll();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question