Yii2 查询条件

时间:2023-03-08 23:12:56
Yii2 查询条件

Model::find()

字符串格式,例如:'status=1'

哈希格式,例如: ['status' => 1, 'type' => 2]

操作符格式,例如:['like', 'name', 'test']

between

//->andFilterWhere(['between','updated_at',$oldTime, $current])
->andWhere(['between','updated_at',$oldTime, $current])

like

//->andFilterWhere(['like','name',$name])
->andWhere(['like','name',$name])

逻辑条件:

//第一种
->Where(['=', 'status',10])
->andWhere(['like', 'title','yii'])
# WHERE (`status` = 10) AND (`title` LIKE '%yii%')
//第二种
->addWhere(['and', 'id=1', 'name=2']);
# WHERE id=1 AND name=2
//第三种
->addWhere(['and', 'type=1', ['or', 'id=1', 'id=2']]);
# WHERE type=1 AND (id=1 OR id=2);
//第四种
->andWhere(['or like','name',['哈哈','苦苦']]);
# WHERE `name` LIKE '%哈哈%' OR `name` LIKE '%苦苦%';
//第五种
->addWhere(['or',['like','name','哈哈'],['like','title','苦苦']]);
# WHERE (`status`=1) AND ((`name` LIKE '%哈哈%') OR (`title` LIKE '%苦苦%'))