搜索
首页后端开发php教程where方法的应用讲解

where方法的应用讲解

Jun 15, 2018 am 10:34 AM
where

where方法的用法是ThinkPHP查询语言的精髓,也是ThinkPHP ORM的重要组成部分和亮点所在,可以完成包括普通查询、表达式查询、快捷查询、区间查询、组合查询在内的查询操作。where方法的参数支持字符串和数组,虽然也可以使用对象但并不建议。

字符串条件

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

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

数组条件

普通查询

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

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

表达式查询

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

表示的查询条件就是 id = 100

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

表示的查询条件就是 id <> 100

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

表示的查询条件就是 id > 100

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

表示的查询条件就是 id >= 100

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

表示的查询条件就是 id < 100

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

表示的查询条件就是 id <= 100

[NOT] LIKE: 同sql的LIKE

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

查询条件就变成 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;);

生成的查询条件就是:(a like '%thinkphp%' OR a like '%tp') AND (b not like '%thinkphp%' AND b not like '%tp')

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

$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();</p>
<p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 19.5px; font-size: 13px; font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(254, 254, 242); text-align: left;">最后得到的查询条件就成了:( `id` != 1 ) AND ( `name` = 'ok' ) AND ( status=1 AND score>10 )</p>
<p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 19.5px; font-size: 13px; font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(254, 254, 242); text-align: left;"><span style="margin:0px;padding:0px;">复合查询</span></p>
<pre style="margin-top:0px;margin-bottom:0px;padding:0px;white-space:pre-wrap;font-family:'Courier New' !important;" 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方法的应用更多相关内容请关注php中文网。

相关推荐:

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

ThinkPHP5快速入门 方法的介绍

介绍ThinkPHP使用步骤

以上是where方法的应用讲解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
高流量网站的PHP性能调整高流量网站的PHP性能调整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依赖注入:初学者的代码示例PHP中的依赖注入:初学者的代码示例May 14, 2025 am 12:08 AM

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

PHP性能:是否可以优化应用程序?PHP性能:是否可以优化应用程序?May 14, 2025 am 12:04 AM

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

PHP性能优化:最终指南PHP性能优化:最终指南May 14, 2025 am 12:02 AM

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

PHP依赖注入容器:快速启动PHP依赖注入容器:快速启动May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依赖注入与服务定位器PHP中的依赖注入与服务定位器May 13, 2025 am 12:10 AM

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

PHP性能优化策略。PHP性能优化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP电子邮件验证:确保正确发送电子邮件PHP电子邮件验证:确保正确发送电子邮件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用