Laravel5.1 框架資料庫查詢構建器用法例項詳解
本文例項講述了Laravel5.1 框架資料庫查詢構建器用法。分享給大家供大家參考,具體如下:
今兒個咱說說查詢構建器。它比執行原生SQL要簡單些,它的操作面兒也是比較廣泛的。
1 查詢結果
先來看看它的語法:
public function getSelect() { $result = DB::table('articles')->get(); dd($result); }
查詢構建器就是通過table方法返回的,使用get()可以返回一個結果集(array型別) 這裡是返回所有的資料,當然你也可以連結很多約束。
1.1 獲取一列/一行資料
public function getSelect() { $result = DB::table('articles')->where('title','learn database')->get(); // 獲取整列資料 $articles = DB::table('articles')->where('title','learn database')->first(); // 獲取一行資料 dd($result,$articles); }
我們可以通過where來增添條件。
1.2 獲取資料列值列表
如果你想要取到某列的值的話 可以使用lists方法:
public function getSelect() { $result = DB::table('articles')->where('id','<',2)->lists('title'); $titles = DB::table('articles')->lists('title'); dd($result,$titles); }
1.3 獲取組塊兒結果集
在我們資料表中資料特別特別多時 可以使用組塊結果集 就是一次獲取一小塊資料進行處理
public function getSelect() { DB::table('articles')->chunk(2,function ($articles){ foreach ($articles as $article){ echo $article->title; echo "<br />"; } }); }
如果說要終止組塊執行的話 返回false就可以了:
public function getSelect() { DB::table('articles')->chunk(2,function ($articles){ return false; }); }
1.4 聚合函式
構建器還提供了很多的實用方法供我們使用:
- count方法:返回構建器查詢到的資料量。
- max方法:傳入一列 返回這一列中最大的值。
- min方法:跟max方法類似,它返回最小的值。
- sum方法:返回一列值相加的和。
- avg方法:計算平均值。
1.4.1 count
public function getArticlesInfo() { $article_count = DB::table('articles')->count(); dd($article_count); }
1.4.2 max
public function getArticlesInfo() { $maxCommentCount = DB::table('articles')->max('comment_count'); dd($maxCommentCount); }
1.4.3 sum
public function getArticlesInfo() { $commentSum = DB::table('articles')->sum('comment_count'); }
1.4.4 avg
public function getArticlesInfo() { $commentAvg = DB::table('articles')->avg('comment_count'); dd($commentAvg); }
1.5 select查詢
1.5.1 自定義子句
select語句可以獲取指定的列,並且可以自定義鍵:
public function getArticlesInfo() { $articles = DB::table('articles')->select('title')->get(); // 輸出結果: // array:3 [▼ // 0 => {#150 ▼ // +"title": "laravel database" // } // 1 => {#151 ▼ // +"title": "learn database" // } // 2 => {#152 ▼ // +"title": "alex" // } // ] $articles1 = DB::table('articles')->select('title as articles_title')->get(); // 輸出結果: // array:3 [▼ // 0 => {#153 ▼ // +"articles_title": "laravel database" // } // 1 => {#154 ▼ // +"articles_title": "learn database" // } // 2 => {#155 ▼ // +"articles_title": "alex" // } // ] $articles2 = DB::table('articles')->select('title as articles_title','id as articles_id')->get(); // array:3 [▼ // 0 => {#156 ▼ // +"articles_title": "laravel database" // +"articles_id": 1 // } // 1 => {#157 ▼ // +"articles_title": "learn database" // +"articles_id": 2 // } // 2 => {#158 ▼ // +"articles_title": "alex" // +"articles_id": 3 // } // ] }
1.5.2 distinct方法
關於distinct方法我還沒弄明白到底是什麼意思 適用於什麼場景,也歡迎大神們給出個答案 謝謝
distinct方法允許你強制查詢返回不重複的結果集。
public function getArticlesInfo() { $articles = DB::table('articles')->distinct()->get(); }
1.5.3 addSelect方法
如果你想要新增一個select 可以這樣做:
public function getArticlesInfo() { $query = DB::table('articles')->select('title as articles_title'); $articles = $query->addSelect('id')->get(); dd($articles); }
2 where語句
where語句是比較常用的,經常用他來進行條件篩選。
2.1 where基礎介紹
現在來詳細介紹下where方法 它接收三個引數:
- 列名,這個沒什麼好說的。
- 資料庫系統支援的操作符,比如說 ”=“、”<“、”like“這些,如果不傳入第二個引數 那麼預設就是”=“等於。
- 要比較的值。
public function getArticlesInfo() { $articles1 = DB::table('articles')->where('id','2')->get(); // 等於 $articles2 = DB::table('articles')->where('id','>','2')->get(); // 大於 $articles3 = DB::table('articles')->where('id','<>','2')->get(); // 不等於 $articles4 = DB::table('articles')->where('id','<=','2')->get(); // 小於等於 $articles5 = DB::table('articles')->where('title','LIKE','%base')->get(); // 類似 }
2.2 orWhere
orWhere和where接收的引數是一樣的,當where邏輯沒有查詢到 or查詢到了 返回or的結果,當where查詢到了 or也查詢到了 返回它們的結果。
public function getArticlesInfo() { $articles = DB::table("articles")->where('id','=','5')->orWhere('title','laravel database')->get(); dd($articles); }
2.3 whereBetween和whereNotBetween
whereBetween是指列值是否在所給定的值之間:
public function getArticlesInfo() { $articles = DB::table("articles")->whereBetween('id',[1,3])->get(); dd($articles); }
↑ 上述程式碼是查詢id在1~3之間的集合。
whereNotBetween和whereBetween相反:
public function getArticlesInfo() { $articles = DB::table("articles")->whereNotBetween('id',3])->get(); dd($articles); }
↑ 上述程式碼是查詢id不在1~3之間的集合。
2.4 whereIn和whereNotIn
whereIn是查詢列值在給定的一組資料中:
public function getArticlesInfo() { $articles = DB::table("articles")->whereIn('id',3,5,8])->get(); dd($articles); }
↑ 上述程式碼中是查詢ID為1,3,5,8的集合,不過我們資料庫中只有id為1和3的資料 那麼它只會返回id為1和3的集合。
whereNotIn和whereIn相反的:
public function getArticlesInfo() { $articles = DB::table("articles")->whereNotIn('id',8])->get(); dd($articles); }
↑ 上述程式碼中是查詢ID不是1,3,5,8的集合。
2.5 whereNull和whereNotNull
whereNull是查詢列值為空的資料:
public function getArticlesInfo() { $articles = DB::table("articles")->whereNull('created_at')->get(); dd($articles); }
↑ 上述程式碼中是查詢created_at為空的集合。
whereNotNull就不用說啦:
public function getArticlesInfo() { $articles = DB::table("articles")->whereNotNull('created_at')->get(); dd($articles); }
↑ 上述程式碼中是查詢created_at不為空的集合。
3 插入資料
先看下最簡單的插入方法:
public function getInsertArticle() { // 插入一條資料: DB::table('articles')->insert( ['title'=>'get more','body'=>'emmmmmm......'] ); // 插入多條資料: DB::table('articles')->insert([ ['title'=>'testTitle1','body'=>'testBody1'],['title'=>'testTitle2','body'=>'testBody2'],// .... ]); }
當你需要拿到插入資料的ID的話,可以使用獲取自增ID的方法:
public function getInsertArticle() { // 插入一條資料: $id = DB::table('articles')->insertGetId( ['title'=>'get more','body'=>'emmmmmm......'] ); dd($id); }
4 更新
public function getUpdateArticle() { $result = DB::table('articles')->whereBetween('id',3])->update(['comment_count'=>0]); dd($result); }
↑ update還可以返回影響了幾條資料。
4.1 加/減快捷方法
public function getUpdateArticle() { $result = DB::table('articles')->whereBetween('id',3])->increment('comment_count',2); dd($result); }
↑ increment接受1~2個引數,第一個引數是列名,第二個引數是可選的表示增加幾(預設是1),上面的語句是:comment_count這一列的值增加2。
public function getUpdateArticle() { $result = DB::table('articles')->whereBetween('id',3])->decrement('comment_count',2); dd($result); }
↑ decrement接受1~2個引數,第一個引數是列名,第二個引數是可選的表示減少幾(預設是1),上面的語句是:comment_count這一列的值減少2。
你以為加減快捷方法只接收兩個引數麼?nonono 它還可以接收第三個引數:
public function getUpdateArticle() { $result = DB::table('articles')->whereBetween('id',2,['title' => 'testUpdate']); dd($result); }
↑ 它還可以在增加/減少時對其他列進行修改。
5 刪除
public function getDeleteArticle() { $result = DB::table('articles')->whereBetween('id',3])->delete(); dd($result); }
↑ 用delete刪除資料,它也返回有多少行被影響。
當你想要刪除所有的列 並且把自增ID歸0的話 可以這麼做:
public function getDeleteArticle() { DB::table('articles')->truncate(); }
6 鎖
查詢構建器還包含一些方法幫助你在select語句中實現”悲觀鎖“。可以在查詢中使用sharedLock方法從而在執行語句時帶一把”共享鎖“。共享鎖可以避免被選擇的行被修改直到事務提交:
DB::table('articles')->where('id',100)->sharedLock()->get();
此外你還可以使用lockForUpdate方法。”for update“鎖避免選擇行被其它共享鎖修改或刪除:
DB::table('articles')->where('id',100)->lockForUpdate()->get();
更多關於Laravel相關內容感興趣的讀者可檢視本站專題:《Laravel框架入門與進階教程》、《php優秀開發框架總結》、《php面向物件程式設計入門教程》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧彙總》
希望本文所述對大家基於Laravel框架的PHP程式設計有所幫助。