YII2 where查询条件整理 [ 2.0 版本 ]
文章涉及where、 addParams 、filterWhere 、andWhere、orWhere、 andFilterWhere()、 orFilterWhere()、andFilterCompare()
但是格式是一样的
字符串格式,例如:'status=1'哈希格式,例如: ['status' => 1, 'type' => 2] 操作符格式,例如:['like', 'name', 'test']
字符串和哈希格式很好理解,我们来看看操作符格式,因为操作符格式可以组成相对复杂的查询语句
最简单的就是官方给的例子
$status = 10; $search = 'yii'; $query->where(['status' => $status]);if (!empty($search)) { $query->andWhere(['like', 'title', $search]); } 生成的语句就是 ... WHERE (`status` = 10) AND (`title` LIKE '%yii%')
操作符格式
[操作符, 操作数1, 操作数2, ...]
第一个参数是操作符
操作符包括and、or、 like、in、 between等
第二个第三个都是操作数
第一种最简单的就是上面提到的例子 andWhere(['like', 'title','搜索的标题']); 生成的语句 ... 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 '%苦苦%'))
以下我们介绍where()方法当中,条件的拼装方式。
1 语法
Yii2用where()方法(当然还有其他方法)来实现条件筛选,语法:
public $this where ( $condition, $params = [] )
$params为可选参数,指定要绑定查询的值。
c o n d i t i o n 为 必 选 参 数 , condition为必选参数,condition为必选参数,condition可以是字符串(如’id=1’)或者数组。
$condition为数组时,有两种格式:
哈希格式:[‘column1’ => value1, ‘column2’ => value2, …]
运算符格式:[operator, operand1, operand2, …]
2 哈希格式
通常,哈希格式的查询条件生成这样的SQL语句:
column1=value1 AND column2=value2 AND …
如果某个值是数组,就会生成IN语句。
如果某个值为null,会用IS NULL来生成语句。
例子:
[‘type’ => 1, ‘status’ => 2] // 生成:(type = 1) AND (status = 2)
[‘id’ => [1, 2, 3], ‘status’ => 2] // 生成:(id IN (1, 2, 3)) AND (status = 2)
[‘status’ => null] // 生成:status IS NULL
3 运算符格式
在运算符格式,Yii会根据指定的运算符生成SQL语句。
运算符有:and、or、not、between、not between、in、not in、like、or like、not like、or not like、exists、not exists、>、<、=、>=、<=、!=等。
3.1 对比
[’>’, ‘id’, 1] // 生成:id > 1
[’<’, ‘id’, 100] // 生成:id < 100
[’=’, ‘id’, 10] // 生成:id = 10
[’>=’, ‘id’, 1] // 生成:id >= 1
[’<=’, ‘id’, 100] // 生成:id <= 100
[’!=’, ‘id’, 10] // 生成:id != 10
具体生成的SQL语句,运算符id会自动加上反斜杠引号`,运算数会自动转义。
3.2 and
[‘and’, ‘id’ => 1, ‘id’ => 2] // 生成:id=1 AND id=2
[‘and’, ‘id=1’, ‘id=2’] // 生成:id=1 AND id=2
[‘and’, ‘type=1’, [‘or’, ‘id=1’, ‘id=2’]] // 生成:type=1 AND (id=1 OR id=2)
在第2条和第3条语句中,列名称和搜索值未用键值关系指定,所以生成的SQL不会添加引号,也不会转义。
3.3 or
[‘or’, [‘type’ => [7, 8, 9]], [‘id’ => [1, 2, 3]]] // 生成:(type IN (7, 8, 9) OR (id IN (1, 2, 3)))
3.4 not
[‘not’, [‘attribute’ => null]] // 生成:NOT (attribute IS NULL)
3.5 between和not between
[‘between’, ‘id’, 1, 10] // 生成:id BETWEEN 1 AND 10
[‘not between’, ‘id’, 1, 10] // 生成:id NOT BETWEEN 1 AND 10
运算符后面的运算数1为数据表列名称,运算数2和运算数3分别为列值范围的最小值和最大值。
3.6 in和not in
[‘in’, ‘id’, [1, 2, 3]] // 生成:id IN (1, 2, 3)
[‘not in’, ‘id’, [1, 2, 3]] // 生成:id NOT IN (1, 2, 3)
运算符后面的运算数1为列名称或DB表达式,运算数2为数组,指定列值所在的范围。
这个方法会为值添加引号,并正确转义。
要生成混合IN条件,列名和列值都设置为数组,并且用列名为列值指定下标:
[‘in’, [‘id’, ‘name’], [[‘id’ => 1, ‘name’ => ‘foo’], [‘id’ => 2, ‘name’ => ‘bar’]]] // 生成:(id, name) IN ((1, ‘foo’), (2, ‘bar’))
另外,还可以用子查询作为IN条件的值,如下:
[‘in’, ‘user_id’, (new Query())->select(‘id’)->from(‘users’)->where([‘active’ => 1])]
3.7 like
[‘like’, ‘name’, ‘tester’] // 生成:name LIKE ‘%tester%’
[‘like’, ‘name’, [‘test’, ‘sample’]] // 生成:name LIKE ‘%test%’ AND name LIKE ‘%sample%’
[‘like’, ‘name’, ‘%tester’, false] // 生成:name LIKE ‘%tester’
// 这是自定义查询方式,要传入值为false的运算数3,并且自行添加%
运算数后面的运算数1为列名称或DB表达式,运算数2为字符串或数组,指定列值查询条件。
这个方法会为值添加引号,并正确转义。
or like、not like、or not like用法和like一样。
[‘or like’, ‘name’, [‘test’, ‘sample’]] // 生成:name LIKE ‘%test%’ OR name LIKE ‘%sample%’
[‘not like’, ‘name’, ‘tester’] // 生成:name NOT LIKE ‘%tester%’
[‘or not like’, ‘name’, [‘test’, ‘sample’]] // 生成:name NOT LIKE ‘%test%’ OR name NOT LIKE ‘%sample%’
3.8 exists
[‘exists’, (new Query())->select(‘id’)->from(‘users’)->where([‘active’ => 1])] // 生成:EXISTS (SELECT “id” FROM “users” WHERE “active”=1)
not exists用法和exists一样。