Yii2.0框架基础--数据查询之AR类方法where()

来源:互联网 发布:知行理工app 编辑:程序博客网 时间:2024/05/20 09:08

User::find()->where($condition)->one()/all();

where条件格式:

 //sql: (type = 1) AND (status = 2)$condition = ['type' => 1,'status' =>2] //sql: (id IN (1,2,3)) AND (status = 2)$condition = ['id' => [1,2,3],'status' =>2] //sql: status IS NULL$condition = ['status' => null]
and: //sql: id = 1 AND id = 2$condition = ['and','id=1','id=2']//sql: type = 1 AND (id = 1 OR id = 2)$condition = ['and','type=1',['or','id=1','id=2']]
or: //sql: (type IN (7,8,9) OR (id IN (1,2,3)))$condition = ['or',['type' => [7,8,9]],['id' => [1,2,3]]]
not: //sql: NOT (attribute IS NULL)$condition = ['not',['attribute' => null]]
between: not between用法相同//sql: id BETWEEN 1 AND 10$condition = ['between','id',1,10]
in: not in用法类似//sql: id IN (1,2,3)$condition = ['in','id',[1,2,3]]//sql: IN条件也适用于多字段$condition = ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]]//也适用于内嵌sql语句$condition = ['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]
like: //sql: name LIKE '%sample%'$condition = ['like','name','sample']//sql: name LIKE '%sample1%' AND name LIKE '%sample2%'$condition = ['like','name',['sample1','sample2']]//sql: name LIKE '%sample'$condition = ['like','name','%sample',false]
exists: not exists用法类似//sql: EXISTS (SELECT "id" FROM "user" WHERE "active" = 1)$condition = ['exists',(new Query())->select('id')->from('user')->where(['active' => 1])]
注: //sql: id >= 10$condition = ['>=','id',10]//sql: id!=10$condition = ['!=','id',10]
0 0
原创粉丝点击