thinkphp3.2框架中where條件查詢用法總結
本文實例講述了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)');
復查的查詢語句
有的時候,我們希望通過一次的查詢就能解決問題,這個時候查詢條件往往比較復雜,但是卻比多次查詢庫來的高效。
實在是搞不定的話就直接用
$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ū)間查詢(一個值得多種情況)
默認是 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. 復合查詢
相當于封裝了新的查詢條件在里面
$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 查詢
如果有設置了讀寫分離的話 query 是查詢 execute是更新保存
M()->query('select * from a');
M()->execute('update a set counts = 3 where id = 1103')
4. 獲取要執(zhí)行的sql 語句
有的時候條件太復雜,比如
id in(xxxxx),這個xxx就是通過一系列操作獲得的結果,嫌麻煩的就直接 都扔進去,寫sql 又長,就直接獲取sql語句扔進去
1.fetchsql
2.buildsql
3.select(false)
M('user')->fetchsql(true)->select();
M('user')->buildsql();
M('user')->select(false);
更多關于thinkPHP相關內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結》、《ThinkPHP常用方法總結》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術總結》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。
相關文章
PHP隨手筆記整理之PHP腳本和JAVA連接mysql數(shù)據(jù)庫
這篇文章主要介紹了PHP隨手筆記整理之PHP腳本和JAVA連接mysql數(shù)據(jù)庫的相關資料,需要的朋友可以參考下2015-11-11
PHP實現(xiàn)PDF轉圖片的詳細過程(使用imagick)
最近有一份pdf文件,需要將其轉換成圖片,所以這篇文章主要給大家介紹了關于PHP實現(xiàn)PDF轉圖片的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2023-01-01
PHP把JPEG圖片轉換成Progressive JPEG的方法
這篇文章主要介紹了PHP把JPEG圖片轉換成Progressive JPEG的方法,本文同時講解了Baseline JPEG和Progressive JPEG的區(qū)別和它們的優(yōu)劣,需要的朋友可以參考下2014-06-06
Laravel 讀取 config 下的數(shù)據(jù)方法
今天小編就為大家分享一篇Laravel 讀取 config 下的數(shù)據(jù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10

