thinkphp3 2 where 條件查詢

2021-09-27 03:59:26 字數 2654 閱讀 2845

thinkphp3.2 where 條件查詢

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

查詢條件

支援的表示式查詢,tp不區分大小寫

含義    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%』

在範圍內包括倆端值    between    0<=id<=10    $where[『id』] = array(『between』,array(『0』,』10』))    where id between 0 and 10

不在範圍內    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』] = 『***x』, 這個代表查詢的時候拼接上 *** 條件,一次性解決問題

$where[『_string』] = 『left join a on a.id = b.id where a.id not in (select id from c)』;

區間查詢(乙個值得多種情況)

預設是 and

$where['id'] =array(array('neq','8'),array('elt','200'),'and'); // 小於等於200 不等於 8

$where['id']  = array(array('neq','8'),'array('neq','10')','or'); // 不等於8或者不等於1012

34復合查詢

相當於封裝了新的查詢條件在裡面

$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)

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

3456

78910

1112

1314

153.sql 查詢

如果有設定了讀寫分離的話 query 是查詢 execute是更新儲存

m()->query(『select * from a』);

m()->execute(『update a set counts = 3 where id = 1103』)

4.獲取要執行的sql 語句

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

1.fetchsql

2.buildsql

3.select(false)

thinkphp中查詢,where條件集合寫法

1 字串模式查詢 string 查詢多個 與 條件中巢狀 與 條件 陣列條件可以和字串條件 採用 string 作為查詢條件 混合使用,例如 user m user 例項化user物件 map id array neq 1 map name ok map string status 1 and sc...

Thinkphp中where 條件的使用

where方法的用法是thinkphp查詢語言的精髓,可以完成包括普通查詢 表示式查詢 快捷查詢 區間查詢 組合查詢在內的查詢操作。where方法的引數支援字串和陣列,雖然也可以使用物件但並不建議。示例 user m user 例項化user物件 user where type 1 and stat...

ThinkPHP3 2 驗證隨記

自動完成 靜態方式 在模型類裡面通過 auto屬性定義處理規則。動態方式 使用模型類的auto方法動態建立自動處理規則。自動驗證 靜態方式 在模型類裡面通過 validate屬性定義驗證規則。動態方式 使用模型類的validate方法動態建立自動驗證規則。定義格式為 array array 驗證欄位...