问题描述
我正在为这个而苦苦挣扎,我尝试了各种组合,但都没有得到我想要的。 所以我的sql代码是
select * from table where organisation_id`= org_id
AND due_at` <= start_date
AND due_at` >= end_date
AND category_id = cat_id
and ((standard_task_template_id is null) or (standard_task_template_id not in (standardQuery))
我的 yii2 代码是
$query = TaskEventAllocation::find()
->where([TaskEventAllocation::tableName().'.organisation_id'=> $organisation_id])
->andWhere(['<=','due_at',$unix_end_date->getTimestamp()])
->andWhere(['>=',$unix_start_date->getTimestamp()])
$query->andWhere(['tc.id' => $cat_or_group_id]);
然后如果我添加
$query->andWhere('and',['is','standard_task_template_id',new \yii\db\Expression('null')],['or',['not in',$standardTaskIds]]);
它回来了
SELECT `t_task_event_allocation`.*
FROM `t_task_event_allocation`
WHERE (`t_task_event_allocation`.`organisation_id`=:qp3)
AND (`due_at` <= :qp4)
AND (`due_at` >= :qp5)
AND (`group_id` != :qp6) A
ND (`tc`.`id`=:qp7)
AND (and)
我试过了
$query->andWhere('or',['and',$standardTaskIds]]);
产生与上面相同但在括号中或在括号中
$query->andWhere(['is','or',$standardTaskIds]);
解决方法
->andWhere([
'or',['standard_task_template_id' => null],['not in','standard_task_template_id',$standardTaskIds]
]);