Home  >  Article  >  PHP Framework  >  thinkphp key methods detailed explanation of where() method

thinkphp key methods detailed explanation of where() method

藏色散人
藏色散人forward
2021-12-09 16:43:013683browse

The usage of

where method is the essence of ThinkPHP query language, and is also an important part and highlight of ThinkPHP ORM. It can complete normal query, expression query, quick query, interval query and combined query. query operations within. The parameters of the where method support strings and arrays. Although objects can also be used, it is not recommended.

String condition

$User = M("User"); // 实例化User对象$User->where('type=1 AND status=1')->select();

SELECT * FROM think_user WHERE type=1 AND status=1

Array condition

Normal query

$User = M("User"); 
// 实例化User对象
$map['name'] = 'thinkphp';
$map['status'] = 1; 
// 把查询条件传入查询方法
$User->where($map)->select();

SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1

Expression query

The query condition represented by
$map['字段1']  = array('表达式','查询条件1');
$map['字段2']  = array('表达式','查询条件2');
$Model->where($map)->select(); 
// 也支持
$map['id']  = array('eq',100);

is id = 100

$map['id']  = array('neq',100);

The query condition represented by id <> 100

# The query condition represented by ##
$map['id']  = array('gt',100);
is id > 100

$map['id']  = array('egt',100);
. The query condition represented by id >= 100

$map['id']  = array('lt',100);
## The query condition represented by
# is id < 100

$map[&#39;id&#39;]  = array(&#39;elt&#39;,100);

The query condition represented is id <= 100

[NOT] LIKE

: LIKE with sql

$map[&#39;name&#39;] = array(&#39;like&#39;,&#39;thinkphp%&#39;);

The query condition becomes name like 'thinkphp%'

$map[&#39;a&#39;] =array(&#39;like&#39;,array(&#39;%thinkphp%&#39;,&#39;%tp&#39;),&#39;OR&#39;);
$map[&#39;b&#39;] =array(&#39;notlike&#39;,array(&#39;%thinkphp%&#39;,&#39;%tp&#39;),&#39;AND&#39;);

The generated query condition is: (a like '%thinkphp%' OR a like '%tp') AND (b not like '%thinkphp%' AND b not like '%tp')

[NOT] BETWEEN

: Same as sql's [not] between, query conditions support strings or arrays, for example:

$map[&#39;id&#39;]  = array(&#39;between&#39;,&#39;1,8&#39;);

$map[&#39;id&#39;]  = array(&#39;between&#39;,array(&#39;1&#39;,&#39;8&#39;));

[NOT] IN: 同sql的[not] in ,查询条件支持字符串或者数组,例如:

$map[&#39;id&#39;]  = array(&#39;not in&#39;,&#39;1,5,8&#39;);
$map[&#39;id&#39;]  = array(&#39;not in&#39;,array(&#39;1&#39;,&#39;5&#39;,&#39;8&#39;));

EXP:表达式,支持更复杂的查询情况

$map[&#39;id&#39;]  = array(&#39;exp&#39;,&#39; IN (1,3,8) &#39;);

等同于

$map[&#39;id&#39;]  = array(&#39;in&#39;,&#39;1,3,8&#39;);

组合查询

$User = M("User"); // 实例化User对象
$map[&#39;id&#39;] = array(&#39;neq&#39;,1);$map[&#39;name&#39;] = &#39;ok&#39;;
$map[&#39;_string&#39;] = &#39;status=1 AND score>10';
$User->where($map)->select();</div>
<p>最后得到的查询条件就成了:( `id` != 1 ) AND ( `name` = 'ok' ) AND ( status=1 AND score>10 ) </p>
<p><strong style="margin:0px;padding:0px;">复合查询</strong></p>
<div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);overflow:auto;font-size:13px;font-family:'Courier New';"><pre class="brush:php;toolbar:false">$where['name']  = array('like', '%thinkphp%');
$where['title']  = array('like','%thinkphp%');
$where['_logic'] = 'or';
$map['_complex'] = $where;$map['id']  = array('gt',1);

等同于

$where['id'] = array('gt',1);
$where['_string'] = ' (name like "%thinkphp%")  OR ( title like "%thinkphp") ';

查询条件是 
( id > 1) AND ( ( name like '%thinkphp%') OR ( title like '%thinkphp%') )

等等这些都是常用的where查询方法。

本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/4994302.html,如需转载请自行联系原作者

The above is the detailed content of thinkphp key methods detailed explanation of where() method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete