Answer the question
In order to leave comments, you need to log in
How to properly style a subquery in YII2?
I can't figure out how to format the subquery correctly.
public function actionIndex() {
$query = new \yii\db\Query();
$subQuery = new \yii\db\Query();
$comments_count = $subQuery->select('comments.deadline_id ')
->where(['comments.deadline_id' => 'deadline.id'])
->from('comments')
->count();
$task = $query->select(['deadline.id','deadline.text','deadline.status','deadline.deadline_date','comments_count' => $comments_count])
->where(['deadline.status' => 1])
->from('deadline')
->all();
// $task = $query->createCommand()->sql;var_dump($task);die();
$taskUncheked = $query->select(['deadline.id','deadline.text','deadline.status','deadline.deadline_date','comments_count' => $comments_count])
->where(['deadline.status' => '0'])
->from('deadline')
->orderBy(['deadline.deadline_date' => SORT_ASC])
->all();
return $task + $taskUncheked;
}
Answer the question
In order to leave comments, you need to log in
$task = $query1->select(['deadline.id', 'deadline.text', 'deadline.status', 'deadline.deadline_date', 'COUNT(`comments`.`deadline_id`) AS comments_count'])
->from('deadline')
->join('LEFT JOIN', 'comments', '`deadline`.`id` = `comments`.`deadline_id`')
->where('`deadline`.`status` = 1')
->groupBy('deadline.id')
->all();
$comments_count = $subQuery->select('comments.deadline_id ')
->where(['comments.deadline_id' => 'deadline.id'])
->from('comments')
->count();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question