本文实例讲述了thinkPHP的表达式查询。分享给大家供大家参考,具体如下:
ThinkPHP 表达式
这里说的表达式,是指 ThinkPHP 框架中特有的表达式。这些表达式用于查询或更新删除等操作的 where条件 及模板标签中。
Where 条件中使用表达式
Where 条件表达式格式为:
其中 $map 是一个普通的数组变量,可以根据自己需求而命名。上述格式中的表达式实际是运算符的意义:
Box-sizing: border-Box; border-bottom-style: solid; border-bottom-color: black; padding-bottom: 0px; border-right-style: solid; widows: 1; text-transform: none; background-color: rgb(255,255,255); border-top-color: black; text-indent: 0px; margin: 0px 0px 8px; padding-left: 0px; border-spacing: 0px; width: 629px; padding-right: 0px; display: block; border-collapse: collapse; font: 12px/1.5em Tahoma,verdana,arial,sans-serif,宋体; white-space: normal; border-top-style: solid; letter-spacing: normal; color: rgb(51,51,51); border-right-color: black; border-left-style: solid; overflow: auto; border-left-color: black; word-break: keep-all; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">
Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(255,255); margin: 0px; padding-left: 0px; padding-right: 0px; font-size: 12px; border-top: rgb(204,204,204) 1px solid; padding-top: 0px">
Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(248,248,248); margin: 0px; padding-left: 0px; padding-right: 0px; font-size: 12px; border-top: rgb(204,204) 1px solid; padding-top: 0px">
Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; background-color: rgb(204,204); font-style: normal; margin: 0px; padding-left: 2px; padding-right: 2px; font-size: 12px; border-top: gray 1px solid; font-weight: 700; border-right: gray 1px solid; padding-top: 2px">TP运算符
Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; background-color: rgb(204,204); font-style: normal; margin: 0px; padding-left: 2px; padding-right: 2px; font-size: 12px; border-top: gray 1px solid; font-weight: 700; border-right: gray 1px solid; padding-top: 2px">sql运算符
Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; background-color: rgb(204,204); font-style: normal; margin: 0px; padding-left: 2px; padding-right: 2px; font-size: 12px; border-top: gray 1px solid; font-weight: 700; border-right: gray 1px solid; padding-top: 2px">例子
Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; background-color: rgb(204,204); font-style: normal; margin: 0px; padding-left: 2px; padding-right: 2px; font-size: 12px; border-top: gray 1px solid; font-weight: 700; border-right: gray 1px solid; padding-top: 2px">实际查询条件
Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">eq Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">=Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">$map['id'] = array('eq',100);Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">等效于:$map['id'] = 100;Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(255,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">neqBox-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">!=Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">$map['id'] = array('neq',246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">id != 100Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(248,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">gt 100Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(255,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">egt== 100Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(248,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">ltBox-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px"><Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">$map['id'] = array('lt',246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">id < 100Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(255,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">eltBox-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px"><=Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">$map['id'] = array('elt',246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">id <= 100Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(248,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">likeBox-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">$map['username'] = array('like','Admin%');Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">username like 'Admin%'Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(255,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">betweenBox-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">between andBox-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">$map['id'] = array('between','1,8');Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">id BETWEEN 1 AND 8Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(248,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">not betweenBox-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">not between andBox-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">$map['id'] = array('not between',246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">id NOT BETWEEN 1 AND 8Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(255,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">inBox-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">$map['id'] = array('in',5,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">id in(1,8)Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(248,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">not inBox-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">$map['id'] = array('not in',246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">id not in(1,8)Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(255,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">and(默认)Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">andBox-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">$map['id'] = array(array('gt',1),array('lt',10)); 1) AND (id < 10)Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(248,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">orBox-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,3),10),'or'); 3) OR (id < 10)Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(255,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">xor(异或)Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">xorBox-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">两个输入中只有一个是true时,结果为true,否则为false,例子略。Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">1 xor 1 = 0Box-sizing: border-Box; padding-bottom: 0px; background-color: rgb(248,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">expBox-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">综合表达式Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,246); font-size: 12px; border-top: gray 1px solid; border-right: gray 1px solid; padding-top: 3px">$map['id'] = array('exp','in(1,3,8)');Box-sizing: border-Box; border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 2px; margin: 0px; padding-left: 5px; padding-right: 0px; background: rgb(246,8');
补充说明
同 sql 一样,ThinkPHP运算符不区分大小写,eq 与 EQ 一样。
between、 in 条件支持字符串或者数组,即下面两种写法是等效的:
exp 表达式
上表中的 exp 不是一个运算符,而是一个综合表达式以支持更复杂的条件设置。exp 的操作条件不会被当成字符串,可以使用任何 sql 支持的语法,包括使用函数和字段名称。
exp 不仅用于 where 条件,也可以用于数据更新,如:
注:对于数字字段的加减,可以直接使
原文链接:https://www.f2er.com/thinkphp/19033.html