主頁 > 知識庫 > thinkphp3.2框架中where條件查詢用法總結(jié)

thinkphp3.2框架中where條件查詢用法總結(jié)

熱門標(biāo)簽:團購網(wǎng)站 科大訊飛語音識別系統(tǒng) 阿里云 Linux服務(wù)器 Mysql連接數(shù)設(shè)置 服務(wù)器配置 電子圍欄 銀行業(yè)務(wù)

本文實例講述了thinkphp3.2框架中where條件查詢用法。分享給大家供大家參考,具體如下:

thinkphp3.2 where 條件查詢

在連貫操作中條件where的操作有時候自己很暈,所以整理下,有助于使用

查詢條件

支持的表達式查詢,tp不區(qū)分大小寫

含義 TP運算符 SQL運算符 例子 實際查詢條件
等于 EQ = $where['id'] = array('EQ','1') id = 2
不等于 NEQ != $where['id'] = array('NEQ','1') id!=2
大于 GT > $where['id'] = array('GT','1') id >1
大于等于 EGT EGT $where['id'] = array('EGT','1') id>=1
小于 $where['id'] = array('lt',1) id 1
小于等于 = = $where['id'] = array('elt',1) id=1
匹配 like like where[′id′]=array(′like′,′where[′id′]=array(′like′,′where['id'] = array('like','begin%')
$where['id'] = array('like','%begin%')
where id like '%begin'
where id like 'begin%'
where id like'%begin%
在范圍內(nèi)包括倆端值 between 0=id=10 $where['id'] = array('between',array('0','10')) where id between 0 and 10
不在范圍內(nèi) not between 0 >id and 1o id $where['id'] = array('not between',array('0','10')) where id not between 0 and 10
在枚舉的值中 in in $where['id'] = array('in',array('1','2','5')) where id in ('1','2','3')
不在枚舉值中 not in not in $where['id'] = array('not in',array('1','2',5)) where id not in ('1','2','5')
exp 表達式查詢,支持SQL語法

exp 是表達式的意思,如果你覺得對于一個值限制條件太多的話就可以用這個

$where['id'] = array('exp','in ( select id from id from tableb)');

復(fù)查的查詢語句

有的時候,我們希望通過一次的查詢就能解決問題,這個時候查詢條件往往比較復(fù)雜,但是卻比多次查詢庫來的高效。

實在是搞不定的話就直接用$where['_string'] = 'xxxx', 這個代表查詢的時候拼接上 xxx 條件,一次性解決問題

$where['_string'] = 'left join A on A.id = b.id where a.id not in (select id from C)';

1. 區(qū)間查詢(一個值得多種情況)

默認(rèn)是 and

$where['id'] =array(array('neq','8'),array('elt','200'),'and'); // 小于等于200 不等于 8
$where['id'] = array(array('neq','8'),'array('neq','10')','or'); // 不等于8或者不等于10

2. 復(fù)合查詢

相當(dāng)于封裝了新的查詢條件在里面

$where['a'] = 5;
$where['b'] = 6;
$where['_logic'] = 'or';

sql:where a = 5 or b = 6;

$condition['c'] = '3';
$condition['d'] = '4'
$condition['_logic'] = 'or'
$where['a'] = 9;
$where['_complex'] = $condition;

sql: where a=9 and (c = 3 or d = 4)

根據(jù)需求,靈活使用(無限套下去)

3. sql 查詢

如果有設(shè)置了讀寫分離的話 query 是查詢 execute是更新保存

M()->query('select * from a');
M()->execute('update a set counts = 3 where id = 1103')

4. 獲取要執(zhí)行的sql 語句

有的時候條件太復(fù)雜,比如 id in(xxxxx),這個xxx就是通過一系列操作獲得的結(jié)果,嫌麻煩的就直接 都扔進去,寫sql 又長,就直接獲取sql語句扔進去

1.fetchsql
2.buildsql
3.select(false)

M('user')->fetchsql(true)->select();
M('user')->buildsql();
M('user')->select(false);

更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。

希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。

您可能感興趣的文章:
  • Thinkphp5.0框架視圖view的模板布局用法分析
  • Thinkphp5.0 框架視圖view的比較標(biāo)簽用法分析
  • Thinkphp5.0框架視圖view的循環(huán)標(biāo)簽用法示例
  • Thinkphp5.0 框架實現(xiàn)控制器向視圖view賦值及視圖view取值操作示例
  • Thinkphp5框架實現(xiàn)獲取數(shù)據(jù)庫數(shù)據(jù)到視圖的方法
  • ThinkPHP視圖查詢詳解
  • thinkphp視圖模型查詢提示ERR: 1146:Table ''db.pr_order_view'' doesn''t exist的解決方法
  • 基于thinkPHP3.2實現(xiàn)微信接入及查詢token值的方法
  • 分享ThinkPHP3.2中關(guān)聯(lián)查詢解決思路
  • thinkphp 3.2框架視圖模型 實例視圖查詢結(jié)果的二維數(shù)組合并操作示例

標(biāo)簽:棗莊 衡水 江蘇 廣元 衢州 大理 蚌埠 萍鄉(xiāng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《thinkphp3.2框架中where條件查詢用法總結(jié)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266