首頁  >  文章  >  後端開發  >  ThinkPHP Where 條件中常用表達式範例詳細介紹

ThinkPHP Where 條件中常用表達式範例詳細介紹

黄舟
黄舟原創
2017-04-01 09:12:181386瀏覽

下面小編就為大家帶來一篇ThinkPHP Where 條件中常用表達式範例(詳解)。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧

Where 條件表達式格式為:

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

其中$map 是一個普通的數組變數,可以根據自己需求而命名。上述格式中的表達式實際上是運算子的意義:

username like 'Admin%'#betweenbetween and$map['id'] = array('between','1,8');id ​​BETWEEN 1 AND 8not betweennot between and$map['id'] = array('not between','1,8' );id ​​NOT BETWEEN 1 AND 8inin$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 53787d6333a5f8cecee20f941c7a2ab0 3) OR (id < 10 )
#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 7c58155ddbcfb0c95677704a224a3942 = array('like','Admin%');
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);

以上是ThinkPHP Where 條件中常用表達式範例詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn