Home >Backend Development >PHP Tutorial >Detailed explanation of where method of ThinkPHP CURD method_PHP tutorial
The most commonly used but also the most complex query method of ThinkPHP CURD operation is the where method. The where method is also one of the coherent operation methods of the model class and is mainly used for query and operation condition setting.
The usage of the where method is the essence of the ThinkPHP query language and an important component and highlight of the ThinkPHP ORM (Object Relational Mapping). It can complete normal queries, expression queries, quick queries, interval queries, and combined queries. query operation. The parameters of the where method support strings and arrays. Although objects can also be used, it is not recommended.
1. String condition
Use string conditions to query and operate directly, for example:
$User = M("User"); // 实例化User对象 $User->where('type=1 AND status=1')->select();
The final generated SQL statement is
SELECT * FROM think_user WHERE type=1 AND status=1
If you use version 3.1 or above, when using string conditions, it is recommended to cooperate with the preprocessing mechanism to ensure more security, for example:
$Model->where("id=%d and username='%s' and xx='%f'",array($id,$username,$xx))->select();
Or use:
$Model->where("id=%d and username='%s' and xx='%f'",$id,$username,$xx)->select();
If the $id variable comes from user submission or URL address, if the input is a non-numeric type, it will be forced to be formatted into a numeric format before querying.
The string preprocessing format type supports specifying numbers, strings, etc. For details, please refer to the parameter description of the vsprintf method.
2. Array condition
The where usage of array conditions is the usage recommended by ThinkPHP.
General query
The simplest array query method is as follows:
$User = M("User"); // 实例化User对象 $map['name'] = 'thinkphp'; $map['status'] = 1; // 把查询条件传入查询方法 $User->where($map)->select();
The final generated SQL statement is
SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1
Expression query
The above query condition is just a simple equality judgment. You can use query expressions to support more SQL query syntax. The usage format of query expressions:
$map['字段1'] = array('表达式','查询条件1'); $map['字段2'] = array('表达式','查询条件2'); $Model->where($map)->select(); // 也支持
Expressions are not case-sensitive. The supported query expressions are as follows, and their respective meanings are:
Expression | Meaning |
---|---|
EQ | Equal (=) |
NEQ | Not equal to (a8093152e673feb7aba1828c43532094) |
GT | Greater than (>) |
EGT | Greater than or equal to (>=) |
LT | Less than (<) |
ELT | Less than or equal to (<=) |
LIKE | Fuzzy query |
[NOT] BETWEEN | (not) interval query |
[NOT] IN | (not in) IN query |
EXP | Expression query, supports SQL syntax |
示例如下:
EQ :等于(=)
例如:
$map['id'] = array('eq',100);
和下面的查询等效
$map['id'] = 100;
表示的查询条件就是 id = 100
NEQ: 不等于(a8093152e673feb7aba1828c43532094)
例如:
$map['id'] = array('neq',100);
表示的查询条件就是 id a8093152e673feb7aba1828c43532094 100
GT:大于(>)
例如:
$map['id'] = array('gt',100);
表示的查询条件就是 id > 100
EGT:大于等于(>=)
例如:
$map['id'] = array('egt',100);
表示的查询条件就是 id >= 100
LT:小于(d2fa3e65463ed9eed074ce58ceaff7b5true必须加在数组的最后,表示当前是多条件匹配,这样查询条件就变成 status= 1 AND title = 'thinkphp' ,查询字段支持更多的,例如:
$map['status&score&title'] =array('1',array('gt','0'),'thinkphp','_multi'=>true);
查询条件就变成 status= 1 AND score >0 AND title = 'thinkphp'
注意:快捷查询方式中“|”和“&”不能同时使用。
区间查询
where方法支持对某个字段的区间查询,例如:
$map['id'] = array(array('gt',1),array('lt',10)) ;
得到的查询条件是: (`id` > 1) AND (`id` 3e39506ed89f9f07f7038c038782c676 3) OR (`id` 9ede9bab97093c03dd2ffc72a07931a3 3)
最后一个可以是AND、 OR或者 XOR运算符,如果不写,默认是AND运算。
区间查询的条件可以支持普通查询的所有表达式,也就是说类似LIKE、GT和EXP这样的表达式都可以支持。另外区间查询还可以支持更多的条件,只要是针对一个字段的条件都可以写到一起,例如:
$map['name'] = array(array('like','%a%'), array('like','%b%'), array('like','%c%'), 'ThinkPHP','or');
最后的查询条件是:
(`name` LIKE '%a%') OR (`name` LIKE '%b%') OR (`name` LIKE '%c%') OR (`name` = 'ThinkPHP')
组合查询
组合查询用于复杂的查询条件,如果你需要在查询的时候同时偶尔使用字符串却又不希望丢失数组方式的灵活的话,可以考虑使用组合查询。
组合查询的主体还是采用数组方式查询,只是加入了一些特殊的查询支持,包括字符串模式查询(_string)、复合查询(_complex)、请求字符串查询(_query),混合查询中的特殊查询每次查询只能定义一个,由于采用数组的索引方式,索引相同的特殊查询会被覆盖。
一、字符串模式查询(采用_string 作为查询条件)
数组条件还可以和字符串条件混合使用,例如:
$User = M("User"); // 实例化User对象 $map['id'] = array('neq',1); $map['name'] = 'ok'; $map['_string'] = 'status=1 AND score>10'; $User->where($map)->select();
最后得到的查询条件就成了:
( `id` != 1 ) AND ( `name` = 'ok' ) AND ( status=1 AND score>10 )
二、请求字符串查询方式
请求字符串查询是一种类似于URL传参的方式,可以支持简单的条件相等判断。
$map['id'] = array('gt','100'); $map['_query'] = 'status=1&score=100&_logic=or';
得到的查询条件是:`id`>100 AND (`status` = '1' OR `score` = '100')
三、复合查询
复合查询相当于封装了一个新的查询条件,然后并入原来的查询条件之中,所以可以完成比较复杂的查询条件组装。
例如:
$where['name'] = array('like', '%thinkphp%'); $where['title'] = array('like','%thinkphp%'); $where['_logic'] = 'or'; $map['_complex'] = $where; $map['id'] = array('gt',1);
查询条件是
( id > 1) AND ( ( name like '%thinkphp%') OR ( title like '%thinkphp%') )
复合查询使用了_complex作为子查询条件来定义,配合之前的查询方式,可以非常灵活的制定更加复杂的查询条件。
很多查询方式可以相互转换,例如上面的查询条件可以改成:
$where['id'] = array('gt',1); $where['_string'] = ' (name like "%thinkphp%") OR ( title like "%thinkphp") ';
最后生成的SQL语句是一致的。
3.多次调用
自3.1.3版本开始,where方法支持多次调用,但字符串条件只能出现一次,例如:
$map['a'] = array('gt',1); $where['b'] = 1; $Model->where($map)->where($where)->where('status=1')->select();
多次的数组条件表达式会最终合并,但字符串条件则只支持一次。