Yii2 查询 where 和 or
Yii2 query with where and or
我正在努力解决这个问题,我尝试了各种组合,但没有任何结果是我想要的。
所以我的 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(['>=', 'due_at', $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', 'standard_task_template_id', $standardTaskIds]]);
它returns
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',['is', 'standard_task_template_id', new \yii\db\Expression('null')],['and', ['not in', 'standard_task_template_id', $standardTaskIds]]);
产生与上面相同的结果,但在括号中或中
$query->andWhere(['is', 'standard_task_template_id', new \yii\db\Expression('null')], 'or', ['not in', 'standard_task_template_id', $standardTaskIds]);
产生一个错误说参数 2 必须是数组类型
->andWhere([
'or',
['standard_task_template_id' => null],
['not in', 'standard_task_template_id', $standardTaskIds]
]);
我正在努力解决这个问题,我尝试了各种组合,但没有任何结果是我想要的。 所以我的 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(['>=', 'due_at', $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', 'standard_task_template_id', $standardTaskIds]]);
它returns
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',['is', 'standard_task_template_id', new \yii\db\Expression('null')],['and', ['not in', 'standard_task_template_id', $standardTaskIds]]);
产生与上面相同的结果,但在括号中或中
$query->andWhere(['is', 'standard_task_template_id', new \yii\db\Expression('null')], 'or', ['not in', 'standard_task_template_id', $standardTaskIds]);
产生一个错误说参数 2 必须是数组类型
->andWhere([
'or',
['standard_task_template_id' => null],
['not in', 'standard_task_template_id', $standardTaskIds]
]);