您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關thinkphp5中常用數據庫查詢語句是什么,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
tp_data 數據表
value()
$name = Db::name('data') -> where('id', 16) -> value('name'); print_r($name); // 獲取 tp_data 數據表中 id = 16,name 字段的值,并打印 // 結果:1111 /** 原生sql語句 >Prepare SELECT `name` FROM `tp_data` WHERE `id` = ? LIMIT 1 >Execute SELECT `name` FROM `tp_data` WHERE `id` = 16 LIMIT 1 */
column()
獲取一列滿足條件的數據
$list = Db::name('data) -> where('status', 1) -> column('name'); print_r($list); // 從 tp_data 數據表獲取一列 status = 1 的 name 字段值 /** 結果: Array( [0] => thinkphp [1] => thinkphp [2] => thinkphp [3] => thinkphp [4] => 7777777777 [5] => thinkphp [6] => thinkphp [7] => thinkphp [8] => thinkphp ) */
獲取一列滿足條件的數據,并以id值為鍵名
$list = Db::name('data) -> where('status', 1) -> column('name', 'id'); print_r($list); // 從 tp_data 數據表獲取一列 status=1 的 name 字段值集合 /** 結果: Array( [3] => thinkphp [4] => thinkphp [5] => thinkphp [6] => thinkphp [7] => 7777777777 [8] => thinkphp [9] => thinkphp [10] => thinkphp [11] => thinkphp ) */
獲取以id為鍵名的數據集
$list = Db::name('data') -> where('status', 1) -> column('*', 'id'); print_r($list); // 從 tp_data 數據表獲取一列 status=1 的數據集 /** 結果: 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
統計 data 表的數據
$count = Db::name('data') -> where('status', 1) -> count(); echo $count; // 結果:9
統計 data 表的最大 id
$max = Db::name('data') -> where('status', 1) -> max('id); echo $max; // 結果:11
簡單查詢
$result = Db::name('data') -> where("id > :id and name like :name", [ 'id' => 10, 'name' => "%php%" ]) -> select(); print_r($result); /** 結果: 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,時間戳格式
查詢時間大于 2016-1-1 的數據
$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); // 從本周星期一開始
查詢最近兩天添加的數據
$result = Db::name('users') -> whereTime('reg_time', '>', '-2 days') -> select(); print_r($result);
查詢創建時間在 2016-1-1 ~ 2017-7-1 的數據
$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 */
查詢今天的數據
昨天: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 */
改進
$p = 0; do { $result = Db::name('data') -> limit($p, 2) -> select(); $p += 2; //處理數據 } while(count($result) > 0);
關于“thinkphp5中常用數據庫查詢語句是什么”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。