發(fā)布日期: 2021-09-29 | 來源: 智軟設(shè)計工作室
有查詢條件就查詢,
多個查詢條件,只要有查詢,就增加一個查詢條件
//類型
if($sotype){
$where['type'] = $sotype;
}
//合作單位
if($companyid){
$where['hezuodanwei'] = $companyid;
}
//關(guān)鍵詞 模糊查詢 $type 是變量
if($key){
$where[$type] = ['like',"%".$key."%"];
}
$rs=Db::name('student')->where($where)->order('id desc')->limit($limit)->page($page)->select();
$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');
組成查詢數(shù)組$where
where($where)
引用:http://blog.csdn.net/u010447573/article/details/47420063
Where 條件表達式格式為:
$map['字段名'] = array('表達式', '操作條件');其中 $map 是一個普通的數(shù)組變量,可以根據(jù)自己需求而命名。上述格式中的表達式實際是運算符的意義:
| 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時,結(jié)果為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 條件支持字符串或者數(shù)組,即下面兩種寫法是等效的:
$map['id'] = array('not in','1,5,8');
$map['id'] = array('not in',array('1','5','8'));上表中的 exp 不是一個運算符,而是一個綜合表達式以支持更復(fù)雜的條件設(shè)置。exp 的操作條件不會被當成字符串,可以使用任何 SQL 支持的語法,包括使用函數(shù)和字段名稱。
exp 不僅用于 where 條件,也可以用于數(shù)據(jù)更新,如:
$Dao = M("Article");
// 構(gòu)建 save 的數(shù)據(jù)數(shù)組,文章點擊數(shù)+1
$data['id'] = 10;
$data['counter'] = array('exp','counter+1');
// 根據(jù)條件保存修改的數(shù)據(jù)
$User->save($data);
| 版本 | 新增功能 |
|---|---|
| 5.0.9 | 比較運算增加閉包子查詢支持 |
| 5.0.4 | 支持對同一個字段多次調(diào)用查詢方法 |
查詢表達式支持大部分的SQL查詢語法,也是ThinkPHP查詢語言的精髓,查詢表達式的使用格式:
where('字段名','表達式','查詢條件');whereOr('字段名','表達式','查詢條件');表達式不分大小寫,支持的查詢表達式有下面幾種,分別表示的含義是:
| 表達式 | 含義 |
|---|---|
| EQ、= | 等于(=) |
| NEQ、<> | 不等于(<>) |
| GT、> | 大于(>) |
| EGT、>= | 大于等于(>=) |
| LT、< | 小于(<) |
| ELT、<= | 小于等于(<=) |
| LIKE | 模糊查詢 |
| [NOT] BETWEEN | (不在)區(qū)間查詢 |
| [NOT] IN | (不在)IN 查詢 |
| [NOT] NULL | 查詢字段是否(不)是NULL |
| [NOT] EXISTS | EXISTS查詢 |
| EXP | 表達式查詢,支持SQL語法 |
| > time | 時間比較 |
| < time | 時間比較 |
| between time | 時間比較 |
| notbetween time | 時間比較 |
表達式查詢的用法示例如下:
例如:
where('id','eq',100);where('id','=',100);和下面的查詢等效
where('id',100);表示的查詢條件就是 id = 100
例如:
where('id','neq',100);where('id','<>',100);表示的查詢條件就是 id <> 100
例如:
where('id','gt',100);where('id','>',100);表示的查詢條件就是 id > 100
例如:
where('id','egt',100);where('id','>=',100);表示的查詢條件就是 id >= 100
例如:
where('id','lt',100);where('id','<',100);表示的查詢條件就是 id < 100
例如:
where('id','elt',100);where('id','<=',100);表示的查詢條件就是 id <= 100
例如:
where('name','like','thinkphp%');查詢條件就變成 name like 'thinkphp%'
V5.0.5+版本開始,like查詢支持使用數(shù)組
where('name','like',['%think','php%'],'OR');查詢條件支持字符串或者數(shù)組,例如:
where('id','between','1,8');和下面的等效:
where('id','between',[1,8]);查詢條件就變成 id BETWEEN 1 AND 8
查詢條件支持字符串或者數(shù)組,例如:
where('id','not in','1,5,8');和下面的等效:
where('id','not in',[1,5,8]);查詢條件就變成 id NOT IN (1,5, 8)
[NOT] IN查詢支持使用閉包方式
查詢字段是否(不)是Null,例如:
where('name', null);where('title','null');where('name','not null');如果你需要查詢一個字段的值為字符串null或者not null,應(yīng)該使用:
where('title','=', 'null');where('name','=', 'not null');支持更復(fù)雜的查詢情況 例如:
where('id','in','1,3,8');可以改成:
where('id','exp',' IN (1,3,8) ');exp查詢的條件不會被當成字符串,所以后面的查詢條件可以使用任何SQL支持的語法,包括使用函數(shù)和字段名稱。
2021-11-11
激活步驟:1.如下圖,以管理員運行;2.輸入:123slmgr /ipk WC2BQ-8NRM3-FDDYY-2BFGV-KHKQYslmgr /skms kms.03k.orgslmgr /ato
閱讀更多2023-07-08
<formaction="{pboot:scaction}"method="get">關(guān)鍵字:<inputtype="text"name="keyword"><inp
閱讀更多2021-11-24
網(wǎng)站安全 pbootcms修改apps目錄 更安全 ,建議不經(jīng)常更新的用戶。修改。如果要升級在改回來。打開 /core/init.php// 定義應(yīng)用存放物理路徑define('APP_PATH', ROOT_PATH . '/apps');修改成復(fù)雜點的。改完后apps目錄也要相應(yīng)改下。
閱讀更多2023-02-22
需要添加一段css代碼:* { -webkit-touch-callout:none;/*系統(tǒng)默認菜單被禁用*/ -webkit-user-select:none;/*webkit瀏覽器*/ -khtml-user-select:none;/*早起瀏覽器*/ -moz-user-select:none;/*火狐瀏覽器*/ -
閱讀更多2023-07-03
今天遇到一個網(wǎng)站有三級欄目,要求是。點擊二級顯示三級子類。點擊三級顯示同級別分類,以下代碼可以實現(xiàn),通過判斷是否有子類。來判斷顯示什么級別內(nèi)容,parent=可以自己按需求來。是下級還是同級還是頂級。{sort:scode}{sort:tcode}{sort:pcode}{pboot:if('{pboot:navpa
閱讀更多2022-12-10
// flex空白元素填充 function flex_empty(listbox, ele, num, emptyclass) { var len = listbox.find(ele).length; var need = Math.ceil(len / num) * num - len; &nb
閱讀更多2025-12-26
最近客戶網(wǎng)站,偶發(fā)出現(xiàn)502錯誤。服務(wù)器配置很多也沒用完。其他進程配置也放很高。配置玩了。還是偶發(fā)會有。后來看到一個大神說的。瀏覽器和nginx的鏈接默認時60秒,然后nginx和應(yīng)用之間比如php或者JAVA默認值沒有60秒。一般是20秒或者30秒,所以就導(dǎo)致這個問題了。值需要在nginx里設(shè)置這個超時時間。小于60秒就行了。 
閱讀更多