这个查询有得优化吗? [ 2.0 版本 ]
$query->andFilterWhere([
'OR',
['id' => $this->location_id],
[
'OR',
['area_id' => $this->location_id],
[
'OR',
['city_id' => $this->location_id],
['province_id' => $this->location_id],
],
],
]);
最佳答案
其他 2 个回答
-
还有一种写法:
$query->andWhere(new OrCondition([ new InCondition('type', 'in', $types), ['like', 'name', '%good%'], 'disabled=false' ]));
点击:配合数据库工作,查找关键字:
OrCondition
看了下源码,
['OR', 操作数1, 操作数2, ...]
最后转换就是用到就是用到类OrCondition
。相关文件及关键代码:
文件:Class yii\db\QueryBuilder代码:
/** * Contains array of default condition classes. Extend this method, if you want to change * default condition classes for the query builder. See [[conditionClasses]] docs for details. * * @return array * @see conditionClasses * @since 2.0.14 */ protected function defaultConditionClasses() { return [ 'NOT' => 'yii\db\conditions\NotCondition', 'AND' => 'yii\db\conditions\AndCondition', 'OR' => 'yii\db\conditions\OrCondition', 'BETWEEN' => 'yii\db\conditions\BetweenCondition', 'NOT BETWEEN' => 'yii\db\conditions\BetweenCondition', 'IN' => 'yii\db\conditions\InCondition', 'NOT IN' => 'yii\db\conditions\InCondition', 'LIKE' => 'yii\db\conditions\LikeCondition', 'NOT LIKE' => 'yii\db\conditions\LikeCondition', 'OR LIKE' => 'yii\db\conditions\LikeCondition', 'OR NOT LIKE' => 'yii\db\conditions\LikeCondition', 'EXISTS' => 'yii\db\conditions\ExistsCondition', 'NOT EXISTS' => 'yii\db\conditions\ExistsCondition', ]; }
共 1 条回复
阿刚 深圳
注册时间:2016-04-04
最后登录:2024-09-04
在线时长:15小时8分
最后登录:2024-09-04
在线时长:15小时8分
- 粉丝4
- 金钱190
- 威望0
- 积分340