您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)thinkphp5中常用數(shù)據(jù)庫查詢語句是什么,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
tp_data 數(shù)據(jù)表
value()
$name = Db::name('data') -> where('id', 16) -> value('name'); print_r($name); // 獲取 tp_data 數(shù)據(jù)表中 id = 16,name 字段的值,并打印 // 結(jié)果:1111 /** 原生sql語句 >Prepare SELECT `name` FROM `tp_data` WHERE `id` = ? LIMIT 1 >Execute SELECT `name` FROM `tp_data` WHERE `id` = 16 LIMIT 1 */
column()
獲取一列滿足條件的數(shù)據(jù)
$list = Db::name('data) -> where('status', 1) -> column('name'); print_r($list); // 從 tp_data 數(shù)據(jù)表獲取一列 status = 1 的 name 字段值 /** 結(jié)果: Array( [0] => thinkphp [1] => thinkphp [2] => thinkphp [3] => thinkphp [4] => 7777777777 [5] => thinkphp [6] => thinkphp [7] => thinkphp [8] => thinkphp ) */
獲取一列滿足條件的數(shù)據(jù),并以id值為鍵名
$list = Db::name('data) -> where('status', 1) -> column('name', 'id'); print_r($list); // 從 tp_data 數(shù)據(jù)表獲取一列 status=1 的 name 字段值集合 /** 結(jié)果: Array( [3] => thinkphp [4] => thinkphp [5] => thinkphp [6] => thinkphp [7] => 7777777777 [8] => thinkphp [9] => thinkphp [10] => thinkphp [11] => thinkphp ) */
獲取以id為鍵名的數(shù)據(jù)集
$list = Db::name('data') -> where('status', 1) -> column('*', 'id'); print_r($list); // 從 tp_data 數(shù)據(jù)表獲取一列 status=1 的數(shù)據(jù)集 /** 結(jié)果: Array( [3] => Array( [id] => 3 [name] => thinkphp [status] => 1 ) [4] => Array( [id] => 4 [name] => thinkphp [status] => 1 ) [5] => Array( [id] => 5 [name] => thinkphp [status] => 1 ) ... ) */
聚合查詢
count
max
min
avg
sum
統(tǒng)計(jì) data 表的數(shù)據(jù)
$count = Db::name('data') -> where('status', 1) -> count(); echo $count; // 結(jié)果:9
統(tǒng)計(jì) data 表的最大 id
$max = Db::name('data') -> where('status', 1) -> max('id); echo $max; // 結(jié)果:11
簡單查詢
$result = Db::name('data') -> where("id > :id and name like :name", [ 'id' => 10, 'name' => "%php%" ]) -> select(); print_r($result); /** 結(jié)果: Array( [0] => Array( [id] => 11 [name] => thinkphp [status] => 1 ) ) */ /** 原生sql語句: >Prepare SELECT * FROM `tp_data` WHERE (id > ? and name like ?) >Execute SELECT * FROM `tp_data` WHERE (id > '10' and name like '%php%') */
日期查詢
日期類型int,時(shí)間戳格式
查詢時(shí)間大于 2016-1-1 的數(shù)據(jù)
$result = Db::name('users') -> whereTime('reg_time', '>', '2016-1-1') -> select(); print_r($result); /** 原生sql語句: >Prepare SELECT * FROM `tp_users` WHERE `reg_time` > ? >Execute SELECT * FROM `tp_users` WHERE `reg_time` > 1451577600 */
查詢本周
$result = Db::name('users') -> whereTime('reg_time', '>', 'this week') -> select(); print_r($result); // 從本周星期一開始
查詢最近兩天添加的數(shù)據(jù)
$result = Db::name('users') -> whereTime('reg_time', '>', '-2 days') -> select(); print_r($result);
查詢創(chuàng)建時(shí)間在 2016-1-1 ~ 2017-7-1 的數(shù)據(jù)
$result = Db::name('users') -> whereTime('reg_time', 'between', ['2016-1-1', '2017-7-1']) -> select(); print_r($result); /** 原生sql語句: >Prepare SELECT * FROM `tp_users` WHERE `reg_time` BETWEEN ? AND ? >Execute SELECT * FROM `tp_users` WHERE `reg_time` BETWEEN 1451577600 AND 1483200000 */
查詢今天的數(shù)據(jù)
昨天:yesterday
本周:week
上周:last week
$result = Db::name('users') -> whereTime('reg_time', 'today') -> select(); print_r($result);
分塊查詢
Db::name('data') -> where('status', '>', 0) -> chunk(2, function($list) { foreach($list as $data) { //處理2條記錄 } }); /** 原生sql語句: >Prepare SELECT * FROM `tp_data` WHERE `status` > ? ORDER BY `id` asc LIMIT 2 >Execute SELECT * FROM `tp_data` WHERE `status` > 0 ORDER BY `id` asc LIMIT 2 >Close stmt >Prepare SELECT * FROM `tp_data` WHERE `status` > ? AND `id` > ? ORDER BY `id` asc LIMIT 2 >Execute SELECT * FROM `tp_data` WHERE `status` > 0 AND `id` > 4 ORDER BY `id` asc LIMIT 2 >Close stmt >Prepare SELECT * FROM `tp_data` WHERE `status` > ? AND `id` > ? ORDER BY `id` asc LIMIT 2 >Execute SELECT * FROM `tp_data` WHERE `status` > 0 AND `id` > 6 ORDER BY `id` asc LIMIT 2 >Close stmt ... >Prepare SELECT * FROM `tp_data` WHERE `status` > ? AND `id` > ? ORDER BY `id` asc LIMIT 2 >Execute SELECT * FROM `tp_data` WHERE `status` > 0 AND `id` > 16 ORDER BY `id` asc LIMIT 2 >Close stmt >Prepare SELECT * FROM `tp_data` WHERE `status` > ? AND `id` > ? ORDER BY `id` asc LIMIT 2 >Execute SELECT * FROM `tp_data` WHERE `status` > 0 AND `id` > 17 ORDER BY `id` asc LIMIT 2 >Close stmt */
改進(jìn)
$p = 0; do { $result = Db::name('data') -> limit($p, 2) -> select(); $p += 2; //處理數(shù)據(jù) } while(count($result) > 0);
關(guān)于“thinkphp5中常用數(shù)據(jù)庫查詢語句是什么”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。