TP5鏈式操作:

TP5的數據庫配置文件:.../application/database.php

引用:https://www.kancloud.cn/manual/thinkphp5

基本操作:

Db::query('select * from think_user where id=?',[8]);
Db::execute('insert into think_user (id, name) values (?, ?)',[8,'thinkphp']);
Db::query('select * from think_user where id=:id',['id'=>8]);
Db::execute('insert into think_user (id, name) values (:id, :name)',['id'=>8,'name'=>'thinkphp']);
Db::connect($config)->query('select * from think_user where id=:id',['id'=>8]);

$config是一個單獨的數據庫配置,支持數組和字符串,也可以是一個數據庫連接的配置參數名。

查詢語句:

查詢單條記錄:

Db::table('think_user')->where('id',1)->find();

find 方法查詢結果不存在,返回 null,如果查詢到了,返回數組。

查詢多條記錄:

Db::table('think_user')->where('status',1)->select();

select 方法查詢結果不存在,返回空數組

 

如果設置了數據表前綴參數的話,可以使用

Db::name('user')->where('id',1)->find();
Db::name('user')->where('status',1)->select();

查詢某個字段的值可以用

 

Db::table('think_user')->where('id',1)->value('name');

value 方法查詢結果不存在,返回 null

查詢某一列的值可以用

Db::table('think_user')->where('status',1)->column('name');

column 方法查詢結果不存在,返回空數組

添加語句:

 

$data = ['foo' => 'bar', 'bar' => 'foo'];
Db::table('think_user')->insert($data);
$userId = Db::name('user')->getLastInsID(); //返回新增數據的自增主鍵

整合:

 

$userId = Db::name('user')->insertGetId($data);
$data = [
    ['foo' => 'bar', 'bar' => 'foo'],
    ['foo' => 'bar1', 'bar' => 'foo1'],
    ['foo' => 'bar2', 'bar' => 'foo2']
];
Db::name('user')->insertAll($data);

insertAll 方法添加數據成功返回添加成功的條數

 

更新語句:

 

 

Db::table('think_user')->where('id', 1)->update(['name' => 'thinkphp']);

如果數據中包含主鍵,可以直接使用:

 

Db::table('think_user')->update(['name' => 'thinkphp','id'=>1]);

update 方法返回影響數據的條數,沒修改任何數據返回 0

更新某個字段的值

 

Db::table('think_user')->where('id',1)->setField('name', 'thinkphp');

setField 方法返回影響數據的條數,沒修改任何數據字段返回 0

 

自增或自減一個字段的值

// score 字段加 1
Db::table('think_user')->where('id', 1)->setInc('score');
// score 字段加 5
Db::table('think_user')->where('id', 1)->setInc('score', 5);
// score 字段減 1
Db::table('think_user')->where('id', 1)->setDec('score');
// score 字段減 5
Db::table('think_user')->where('id', 1)->setDec('score', 5);

刪除語句:

 

// 根據主鍵刪除
Db::table('think_user')->delete(1);
Db::table('think_user')->delete([1,2,3]);

// 條件刪除    
Db::table('think_user')->where('id',1)->delete();
Db::table('think_user')->where('id','<',10)->delete();

    獲取鏈式操作的SQL源生語句:

 Db::table('think_user')->fetchSql(true)->delete(1);

   

Limit && page:

 // 從ID=1,開始截取5條記錄

 Db::table('think_user')->limit(1,5)->select();

 Db::table('think_user')->limit('1,5')->select();

 // 截取5條記錄

 Db::table('think_user')->limit(5)->select();

  // 從查詢第一頁數據,一頁顯示5條

 Db::table('think_user')->page(1,5)->select();

 Db::table('think_user')->page('1,5')->select();

 Db::table('think_user')->limit(5)->page(1)->select();

 

引用:https://blog.csdn.net/haibo0668/article/details/78203170

where使用:

$map['字段名']  = array('表達式', '操作條件');

 

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');

引用:https://www.kancloud.cn/manual/thinkphp5

TP5鏈式操作

 

系統支持的鏈式操作方法有:

連貫操作 作用 支持的參數類型
where* 用於AND查詢 字符串、數組和對象
whereOr* 用於OR查詢 字符串、數組和對象
wheretime* 用於時間日期的快捷查詢 字符串
table 用於定義要操作的數據表名稱 字符串和數組
alias 用於給當前數據表定義別名 字符串
field* 用於定義要查詢的字段(支持字段排除) 字符串和數組
order* 用於對結果排序 字符串和數組
limit 用於限制查詢結果數量 字符串和數字
page 用於查詢分頁(內部會轉換成limit) 字符串和數字
group 用於對查詢的group支持 字符串
having 用於對查詢的having支持 字符串
join* 用於對查詢的join支持 字符串和數組
union* 用於對查詢的union支持 字符串、數組和對象
view* 用於視圖查詢 字符串、數組
distinct 用於查詢的distinct支持 布爾值
lock 用於數據庫的鎖機制 布爾值
cache 用於查詢緩存 支持多個參數
relation* 用於關聯查詢 字符串
with* 用於關聯預載入 字符串、數組
bind* 用於數據綁定操作 數組或多個參數
comment 用於SQL註釋 字符串
force 用於數據集的強制索引 字符串
master 用於設置主服務器讀取數據 布爾值
strict 用於設置是否嚴格檢測字段名是否存在 布爾值
sequence 用於設置Pgsql的自增序列名 字符串
failException 用於設置沒有查詢到數據是否拋出異常 布爾值
partition 用於設置分表信息 數組 字符串

whereOR示例:

SQL:SELECT * FROM table_name WHERE id > 5 OR pid  >10 ;
鏈式:Db::name('table_name')->where([id=>['>',5]])->whereOR(['pid'=>['>',10]])->select();

SQL:
SELECT * FROM table_name WHERE (id>=9 AND name='張三') OR (id<9 AND name="李四") ;
鏈式寫法:
$where = [
    'id' => ['>=', 9],
    'name' => '張三'
];
$where_or = [
    'id' => ['<', 9],
    'name' => '李四'
];
Db::name('table_name')->WHERE(function($query) use ($where) {
    $query->where($where);
})->whereOr(function($query) use ($where_or) {
    $query->where($where_or);
})->select();

 

TP5聚合

 

count 統計數量,參數是要統計的字段名(可選)
max 獲取最大值,參數是要統計的字段名(必須)
min 獲取最小值,參數是要統計的字段名(必須)
avg 獲取平均值,參數是要統計的字段名(必須)
sum 獲取總分,參數是要統計的字段名(必須)

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章