ThinkPHP Where 條件中常用表達式示例(詳解)
Where 條件表達式格式為:
$map['字段名'] = array('表達式', '操作條件');
其中 $map 是一個普通的數組變量,可以根據自己需求而命名。上述格式中的表達式實際是運算符的意義:
| 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運算符不區(qū)分大小寫,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 條件中常用表達式示例(詳解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
自己寫的兼容低于PHP 5.5版本的array_column()函數
這篇文章主要介紹了自己寫的兼容低于PHP 5.5版本的array_column()函數,array_column是PHP 5.5新增函數,有時在低版本中也可能要用到,需要的朋友可以參考下2014-10-10
PHP 開發(fā)環(huán)境配置(Zend Server安裝)
運行安裝文件(ZendServer-CE-php-5.3.2-5.0.1-Windows_x86.exe)開始安裝,選項請參照我的選擇。2010-04-04
require(),include(),require_once()和include_once()的異同
require(),include(),require_once()和include_once()的異同...2007-01-01

