ThinkPHP Where 条件中常用表达式示例(详解)

yipeiwu_com5年前PHP代码库

Where 条件表达式格式为:

$map['字段名'] = array('表达式', '操作条件');

其中 $map 是一个普通的数组变量,可以根据自己需求而命名。上述格式中的表达式实际是运算符的意义:

ThinkPHP运算符 与 SQL运算符 对照表
TP运算符 SQL运算符 例子 实际查询条件
eq = $map['id'] = array('eq',100); 等效于:$map['id'] = 100;
neq != $map['id'] = array('neq',100); id != 100
gt > $map['id'] = array('gt',100); id > 100
egt >= $map['id'] = array('egt',100); id >= 100
lt < $map['id'] = array('lt',100); id < 100
elt <= $map['id'] = array('elt',100); id <= 100
like like $map<'username'> = array('like','Admin%'); username like 'Admin%'
between between and $map['id'] = array('between','1,8'); id BETWEEN 1 AND 8
not between not between and $map['id'] = array('not between','1,8'); id NOT BETWEEN 1 AND 8
in in $map['id'] = array('in','1,5,8'); id in(1,5,8)
not in not in $map['id'] = array('not in','1,5,8'); id not in(1,5,8)
and(默认) and $map['id'] = array(array('gt',1),array('lt',10)); (id > 1) AND (id < 10)
or or $map['id'] = array(array('gt',3),array('lt',10), 'or'); (id > 3) OR (id < 10)
xor(异或) xor 两个输入中只有一个是true时,结果为true,否则为false,例子略。 1 xor 1 = 0
exp 综合表达式 $map['id'] = array('exp','in(1,3,8)'); $map['id'] = array('in','1,3,8');

补充说明

• 同 SQL 一样,ThinkPHP运算符不区分大小写,eq 与 EQ 一样。

• between、 in 条件支持字符串或者数组,即下面两种写法是等效的:

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

exp 表达式

上表中的 exp 不是一个运算符,而是一个综合表达式以支持更复杂的条件设置。exp 的操作条件不会被当成字符串,可以使用任何 SQL 支持的语法,包括使用函数和字段名称。

exp 不仅用于 where 条件,也可以用于数据更新,如:

$Dao = M("Article");
//构建 save 的数据数组,文章点击数+1
$data['id'] = 10;
$data['counter'] = array('exp','counter+1');
//根据条件保存修改的数据
$User->save($data);

以上这篇ThinkPHP Where 条件中常用表达式示例(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【宜配屋www.yipeiwu.com】。

相关文章

教你在PHPStorm中配置Xdebug

教你在PHPStorm中配置Xdebug

本教程适用于Laravel项目的使用者,并默认使用Homestead作为开发环境的虚拟机。 1. 确认你已经安装了Xdebug 通过ssh登录你的homestead,执行 ls /etc...

php下连接mssql2005的代码

1.下载以下两个文件,放入php ext目录及system32 php_sqlsrv_52_ts_vc6.dll (线程安全) php_sqlsrv_52_nts_vc6.dll (非线...

如何判断php数组的维度

如下所示:复制代码 代码如下:<?php /**  * 返回数组的维度  * @param  [type] $arr [description] &n...

Yii2使用驼峰命名的形式访问控制器(实例讲解)

Yii2使用驼峰命名的形式访问控制器(实例讲解)

yii2在使用的时候,访问控制器的时候,如果控制器的名称是驼峰命名法,那访问的url中要改成横线的形式。例如: public function actionRoomUpdate()...

解决nginx不支持thinkphp中pathinfo的问题

解决nginx不支持thinkphp中pathinfo的问题

下面小编通过文字加代码的方式给大家详解下,具体内容如下: 其实,要解决nginx不支持pathinfo的问题,有两个解决思路,一是不使用pathinfo模式,二是修改nginx的配置文件...