php基礎知識點 (數組查詢where)


有查詢條件就查詢,

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

 

 
  1.       //類型  
  2. if($sotype){  
  3.     $where['type'] = $sotype;  
  4. }  
  5.  //合作單位  
  6. if($companyid){  
  7.     $where['hezuodanwei'] = $companyid;  
  8. }  
  9. //關鍵詞 模糊查詢 $type 是變量  
  10. if($key){  
  11.         $where[$type] = ['like',"%".$key."%"];  
  12. }  
  13.   
  14. $rs=Db::name('student')->where($where)->order('id desc')->limit($limit)->page($page)->select();  
  15. $rs1=Db::name('student')->where($where)->select();  



 

$where['type'] = $sotype;

$where['hezuodanwei'] = $companyid;

$where["username"] = ['like',"%".$tag["kw"]."%"];//模糊查詢

$where[]=['exp','FIND_IN_SET(2,needID)'];

 

例子:id in(1,5,8)

$where['hezuodanwei'] =array('in','10,12');

 

組成查詢數組$where

 

where($where) 

 

 

引用:http://blog.csdn.net/u010447573/article/details/47420063

 

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 < $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 一樣。
  • 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);

 

 

 

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

 

查詢表達式

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

 

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

where('字段名','表達式','查詢條件'); 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 時間比較

表達式查詢的用法示例如下:

EQ :等於(=)

例如:

where('id','eq',100); where('id','=',100); 

和下面的查詢等效

where('id',100); 

表示的查詢條件就是 id = 100

NEQ: 不等於(<>)

例如:

where('id','neq',100); where('id','<>',100); 

表示的查詢條件就是 id <> 100

GT:大於(>)

例如:

where('id','gt',100); where('id','>',100); 

表示的查詢條件就是 id > 100

EGT:大於等於(>=)

例如:

where('id','egt',100); where('id','>=',100); 

表示的查詢條件就是 id >= 100

LT:小於(<)

例如:

where('id','lt',100); where('id','<',100); 

表示的查詢條件就是 id < 100

ELT: 小於等於(<=)

例如:

where('id','elt',100); where('id','<=',100); 

表示的查詢條件就是 id <= 100

[NOT] LIKE: 同sql的LIKE

例如:

where('name','like','thinkphp%'); 

查詢條件就變成 name like 'thinkphp%'

V5.0.5+版本開始,like查詢支持使用數組

where('name','like',['%think','php%'],'OR'); 

[NOT] BETWEEN :同sql的[not] between

查詢條件支持字符串或者數組,例如:

where('id','between','1,8'); 

和下面的等效:

where('id','between',[1,8]); 

查詢條件就變成 id BETWEEN 1 AND 8

[NOT] IN: 同sql的[not] in

查詢條件支持字符串或者數組,例如:

where('id','not in','1,5,8'); 

和下面的等效:

where('id','not in',[1,5,8]); 

查詢條件就變成 id NOT IN (1,5, 8)

[NOT] IN查詢支持使用閉包方式

[NOT] NULL :

查詢字段是否(不)是Null,例如:

where('name', null); where('title','null'); where('name','not null'); 

如果你需要查詢一個字段的值為字符串null或者not null,應該使用:

where('title','=', 'null'); where('name','=', 'not null'); 

EXP:表達式

支持更復雜的查詢情況 例如:

where('id','in','1,3,8'); 

可以改成:

where('id','exp',' IN (1,3,8) '); 

exp查詢的條件不會被當成字符串,所以后面的查詢條件可以使用任何SQL支持的語法,包括使用函數和字段名稱。

 

其他:

數組表達式:

Db::table('think_user')
->where([
'name' => ['like','thinkphp%'],
'title' => ['like','%thinkphp'],
'id' => ['>',0],
'status'=> 1
])
->select();

 

轉自:https://www.cnblogs.com/haohaoyuan/p/8416382.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM