Rob*_*ini 3 where query-builder yii2
我有一个 Yii2 查询,但orWhereYii 语句有问题。
我需要以下人员:
年龄在 21 岁以下。
OR 年龄在 21 岁以上, AND 名字是约翰。
这是我的代码。我不知道如何在 Yii2 中使用括号作为优先级:
Persons::find()
->select([Persons::tableName().".[[first_name]]"])
->where(['<', 'age', 21])
->orWhere(['>', 'age', 21])
->andwhere([Persons::tableName().".[[first_name]]" => 'John'])
Run Code Online (Sandbox Code Playgroud)
最简单的方法是使用一个具有正确嵌套的数组:
Persons::find()
->select([Persons::tableName() . ".[[first_name]]"])
->where([
'or',
['<', 'age', 21],
[
'and',
['>', 'age', 21],
[Persons::tableName() . ".[[first_name]]" => 'John'],
],
]);
Run Code Online (Sandbox Code Playgroud)
andwhere()并且orWhere()总是将新条件附加到现有条件,所以你会得到类似的东西:
(((<first condition>) AND <second condition>) OR <third condition>)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
648 次 |
| 最近记录: |