正确的Mysql请求
SELECT `task`.`type` AS `type`, `task`.`user_author_id` AS `user_author_id`
FROM `task_goal`
LEFT JOIN `user`
ON `task_goal`.`user_author_id` = `user`.`id`
LEFT JOIN `task`
ON (`task_goal`.`id` = `task`.`goal_id`) AND CASE WHEN `task`.`type` = 'personal' and `task`.`user_author_id` != `user`.`id`
THEN `user`.`show_personal_task` = 1
ELSE true
END;
Yii2请求task_goal并加入HasMany关系“tasks”
$query = TaskGoal::find();
$query->select(['task_goal.*','task_goal.type AS goal_type'])->andWhere(['task_goal.active'=>true])
->joinWith([
'tasks'=>function(ActiveQuery $itemQuery) {
$itemQuery->andOnCondition(['task.active' => true]);
}]);
关系
public function getTasks() {
return $this->hasMany(Task::class, ['goal_id' => 'id']);
}
如何添加CASE WHEN…
CASE WHEN `task`.`type` = 'personal' and `task`.`user_author_id` != `user`.`id`
THEN `user`.`show_personal_task` = 0
ELSE true
END
和“任务”有很多关系吗?
1条答案
按热度按时间3wabscal1#
当我测试andOnCondition时,我添加了[ ]:),所以不要
并细请