首页  >  文章  >  后端开发  >  ThinkPHP中Where 条件中常用的表达式

ThinkPHP中Where 条件中常用的表达式

不言
不言原创
2018-06-07 14:24:481695浏览

下面为大家带来一篇ThinkPHP Where 条件中常用的表达式。内容挺不错的,现在就分享给大家,也给大家做个参考。

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 99cceb643fd24ad4a71fb994e9531ae5 = 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 6e99feeb5d4ab6634ee52e4511733d89 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[&#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 表达式

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

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

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

以上就是本篇文章的全部内容了,感谢大家的阅读,更多相关的内容请关注PHP中文网!

相关推荐:

thinkPHP引入类的方法

ThinkPHP中where()使用方法详解

以上是ThinkPHP中Where 条件中常用的表达式的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn