TP5 where數組查詢(模糊查詢--多個查詢條件)

有查詢條件就查詢,多個查詢條件,只要有查詢,就增長一個查詢條件php

1、TP5.1版本html

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 同樣。thinkphp

  • between、 in 條件支持字符串或者數組,即下面兩種寫法是等效的:數組

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

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

exp 表達式

上表中的 exp 不是一個運算符,而是一個綜合表達式以支持更復雜的條件設置。exp 的操做條件不會被當成字符串,能夠使用任何 SQL 支持的語法,包括使用函數和字段名稱。函數

exp 不只用於 where 條件,也能夠用於數據更新,如:thinkphp5

官方查詢語法:https://www.kancloud.cn/manual/thinkphp5/135182spa

版本 新增功能
5.0.9 比較運算增長閉包子查詢支持
5.0.4 支持對同一個字段屢次調用查詢方法

查詢表達式支持大部分的SQL查詢語法,也是ThinkPHP查詢語言的精髓,查詢表達式的使用格式:.net

   
  1. where('字段名','表達式','查詢條件');

  2. whereOr('字段名','表達式','查詢條件');

表達式不分大小寫,支持的查詢表達式有下面幾種,分別表示的含義是:

表達式 含義
EQ、= 等於(=)
NEQ、<> 不等於(<>)
GT、> 大於(>)
EGT、>= 大於等於(>=)
LT、< 小於(<)
ELT、<= 小於等於(<=)
LIKE 模糊查詢
[NOT] BETWEEN (不在)區間查詢
[NOT] IN (不在)IN 查詢
[NOT] NULL 查詢字段是否(不)是NULL
[NOT] EXISTS EXISTS查詢
EXP 表達式查詢,支持SQL語法
> time 時間比較
< time 時間比較
between time 時間比較
notbetween time 時間比較

tp5.1與tp5.0初始化控制器區別




本文分享自微信公衆號 - 風帆(wdswhf)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索