Application explanation of where method

jacklove
Release: 2023-04-01 08:48:02
Original
2373 people have browsed it

The usage of

where method is the essence of ThinkPHP query language and an important part and highlight of ThinkPHP ORM. It can complete query operations including ordinary query, expression query, quick query, interval query and combined query. 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();
Copy after login

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();
Copy after login

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

Expression query

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

represents the query condition represented by id = 100

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

The query condition is id <> 100

$map['id'] = array('gt',100);
Copy after login

represents the query condition is id > 100

$map['id'] = array('egt',100);
Copy after login

represents the query condition is id >= 100

$map['id'] = array('lt',100);
Copy after login

represents The query condition is id < 100

$map['id'] = array('elt',100);
Copy after login

The query condition represented is id <= 100

[NOT] LIKE: Same as sql's LIKE

$map['name'] = array('like','thinkphp%');
Copy after login

The query condition becomes name like 'thinkphp%'

$map['a'] =array('like',array('%thinkphp%','%tp'),'OR');$map['b'] =array('notlike',array('%thinkphp%','%tp'),'AND');
Copy after login

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['id'] = array('between','1,8');
Copy after login
$map['id'] = array('between',array('1','8'));
Copy after login

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

$map['id'] = array('not in','1,5,8');
Copy after login
$map['id'] = array('not in',array('1','5','8'));
Copy after login

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

$map['id'] = array('exp',' IN (1,3,8) ');
Copy after login

等同于

$map['id'] = array('in','1,3,8');
Copy after login

组合查询

$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 )

复合查询

$where['name'] = array('like', '%thinkphp%');$where['title'] = array('like','%thinkphp%');$where['_logic'] = 'or';$map['_complex'] = $where;$map['id'] = array('gt',1);
Copy after login

等同于

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

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

本文讲解了where方法的应用更多相关内容请关注php中文网。

相关推荐:

ThinkPHP 双重循环遍历输出 的相关内容

ThinkPHP5快速入门 方法的介绍

介绍ThinkPHP使用步骤

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!