如何在 Laravel 的流暢查詢構建器中選擇計數?
Laravel 中的流暢查詢構建器是一個介面,負責建立和執行資料庫查詢。查詢構建器可以很好地與 Laravel 支援的所有資料庫一起使用,並且可以用於在其上執行幾乎所有資料庫操作。
使用流暢查詢構建器的優勢在於它具有防止 SQL 注入攻擊的保護機制。它使用 PDO 引數繫結,您可以自由傳送所需的字串。
流暢查詢構建器支援許多方法,例如count、min、max、avg、sum,這些方法可以從您的表中獲取聚合值。
現在讓我們看一下如何使用流暢查詢構建器在 select 查詢中獲取計數。要使用流暢查詢構建器,請使用如下所示的 DB facade 類
use Illuminate\Support\Facades\DB;
現在讓我們檢查一些示例以在 select 查詢中獲取計數。假設我們已經建立了一個名為 students 的表,使用以下查詢
CREATE TABLE students( id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(15) NOT NULL, email VARCHAR(20) NOT NULL, created_at VARCHAR(27), updated_at VARCHAR(27), address VARCHAR(30) NOT NULL );
並按如下所示填充它:
+----+---------------+------------------+-----------------------------+-----------------------------+---------+ | id | name | email | created_at | updated_at | address | +----+---------------+------------------+-----------------------------+-----------------------------+---------+ | 1 | Siya Khan | siya@gmail.com | 2022-05-01T13:45:55.000000Z | 2022-05-01T13:45:55.000000Z | Xyz | | 2 | Rehan Khan | rehan@gmail.com | 2022-05-01T13:49:50.000000Z | 2022-05-01T13:49:50.000000Z | Xyz | | 3 | Rehan Khan | rehan@gmail.com | NULL | NULL | testing | | 4 | Rehan | rehan@gmail.com | NULL | NULL | abcd | +----+---------------+------------------+-----------------------------+-----------------------------+---------+
表中的記錄數為 4。
示例 1
在下面的示例中,我們在 DB::table 中使用 students。count() 方法負責返回表中存在的總記錄數。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class StudentController extends Controller{ public function index() { $count = DB::table('students')->count(); echo "The count of students table is :".$count; } }
輸出
上述示例的輸出為:
The count of students table is :4
示例 2
在這個例子中,我們將使用selectRaw()來獲取表中存在的總記錄數。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function index() { $count = DB::table('students')->selectRaw('count(id) as cnt')->pluck('cnt'); echo "The count of students table is :".$count; } }
列 id 用於selectRaw()方法中的count()中,並使用 pluck 獲取計數。
輸出
上述程式碼的輸出為:
The count of students table is :[4]
示例 3
此示例將使用selectRaw()方法。假設您想計算名稱,例如Rehan Khan。讓我們看看如何將 selectRaw() 與count()方法一起使用
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function index() { $count = DB::table('students')-> where('name', 'Rehan Khan')-> selectRaw('count(id) as cnt')->pluck('cnt'); echo "The count of name:Rehan Khan in students table is :".$count; } }
在上面的示例中,我們想要查詢表:students中名稱為Rehan Khan的計數。因此,編寫的查詢為。
DB::table('students')->where('name', 'Rehan Khan')->selectRaw('count(id) as cnt')->pluck('cnt');
我們使用了 selectRaw() 方法,該方法負責計算來自 where 過濾器的記錄。最後,pluck() 方法用於獲取計數值。
輸出
上述程式碼的輸出為:
The count of name:Rehan Khan in students table is :[2]
示例 4
如果您計劃使用count()方法來檢查表中是否存在任何記錄,則可以使用 exists() 或doesntExist()方法作為替代,如下所示:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class StudentController extends Controller{ public function index() { if (DB::table('students')->where('name', 'Rehan Khan')->exists()) { echo "Record with name Rehan Khan Exists in the table :students"; } } }
輸出
上述程式碼的輸出為:
Record with name Rehan Khan Exists in the table :students
示例 5
使用doesntExist()方法檢查給定表中是否存在任何記錄。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class StudentController extends Controller{ public function index() { if (DB::table('students')->where('name', 'Neha Khan')->doesntExist()) { echo "Record with name Rehan Khan Does not Exists in the table :students"; } else { echo "Record with name Rehan Khan Exists in the table :students"; } } }
輸出
上述程式碼的輸出為:
Record with name Rehan Khan Does not Exists in the table :students
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP