一、實現原生sql查詢
$list = DB::select($sql); //結果集是json格式
二、實現原生數據庫連接
$dsn = 'mysql:host=127.0.0.1;dbname=mysql';
$user = env('DB_USERNAME','');
$pass = env('DB_PASSWORD','');
$db = new \PDO($dsn, $user, $pass); //連接
$db->exec("SET names utf8"); //設置uft8,exec可以執行所需的sql語句
三、檢索表中的一行(原生)
$user = DB::table('users')->where('name', 'John')->first();
var_dump($user->name);
四、檢索表中的所有行(原生)
$users = DB::table('users')->get();
foreach ($users as $user)
{
var_dump($user->name);
}
五、檢索單個列的行(原生)
$name = DB::table('users')->where('name', 'John')->pluck('name');
六、 指定一個Select子句(查詢所需字段)
$users = DB::table('users')->select('name', 'email')->get();
$users = DB::table('users')->distinct()->get();
$users = DB::table('users')->select('name as user_name')->get();
七、Select子句添加到一個現有的查詢
$query = DB::table('users')->select('name');
$users = $query->addSelect('age')->get();
八、where
$users = DB::table('users')->where('votes', '>', 100)->get();
九、OR
$users = DB::table('users')->orWhere('votes', '>', 100)->get();
十、Where Between
$users = DB::table('users')->whereBetween('votes', array(1, 100))->get();
十一、Where Not Between
$users = DB::table('users')->whereNotBetween('votes', array(1, 100))->get();
十二、Where In
$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();
$users = DB::table('users')->whereNotIn('id', array(1, 2, 3))->get();
十三、Order By, Group By, And Having
$users = DB::table('users')->orderBy('name', 'desc')->groupBy('count')->having('count', '>', 100)->get();
十四、Offset & Limit
$users = DB::table('users')->skip(10)->take(5)->get();
十五、左連接語句
DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->get();
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);
})
->get();
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
十六、高級的where子句
DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
十七、count&max&min&avg&sum
$users = DB::table('users')->count();
$price = DB::table('orders')->max('price');
$price = DB::table('orders')->min('price');
$price = DB::table('orders')->avg('price');
$total = DB::table('users')->sum('votes');
十八、遞增或遞減一個列的值
DB::table('users')->increment('votes', 5);
DB::table('users')->decrement('votes');
十九、指定額外的列更新:
DB::table('users')->increment('votes', 1, array('name' => 'John'));
二十、插入數據
DB::table('users')->insert(
array('email' => 'john@example.com', 'votes' => 0)
);
二十一、插入一個記錄并獲得當前插入的id(需是只增id)
$id = DB::table('users')->insertGetId(
array('email' => 'john@example.com', 'votes' => 0)
);
二十二、更新操作
DB::table('users')
->where('id', 1)
->update(array('votes' => 1));
GoodsRobot::where('good_id', $v['goodId'])->update(array('order_time' => $time));//更詳下單時間
二十三、刪除操作
DB::table('users')->where('votes', '<', 100)->delete();
二十四、刪除表中的所有記錄
DB::table('users')->delete();
二十四、刪除一個表
DB::table('users')->truncate();
二十五、查詢構建器還提供了一種快速的方法來“聯盟”兩個查詢:
$first = DB::table('users')->whereNull('first_name');
$users =DB::table('users')->whereNull('last_name')->union($first)->get();
二十六、SELECT語句“共享鎖”,你可以使用sharedLock方法查詢:
DB::table('users')->where('votes', '>',100)->sharedLock()->get();
二十七、更新“鎖”在一個SELECT語句,您可以使用lockForUpdate方法查詢:
DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();
二十八、緩存查詢
$users = DB::table('users')->remember(10)->get();
二十九、原樣輸出
{!! $buyerShareInstruction !!}
三十、獲取指定字段
ActivitySeller::where('seller_id', $sellerId)->lists('activity_id');//返回的是一維數組
三十一、高級用法
$list->where(function($sql) use($activity_ids){
$sql->where(function($query){
$query->where('is_system', 1)->where('use_seller', 0)->where('seller_id', 0);
})
->orWhere(function($query) use($activity_ids){
$query->where('is_system', 1)->where('use_seller', 1)->whereIn('id', $activity_ids);
});
});
三十二、獲取sql
DB::connection()->enableQueryLog();
中間是sql執行體(5.0之后怎么用的哈,5.0之前不是這樣用哈)
dd(DB::getQueryLog());
- 環境搭建
- centos6.5 lnmp環境搭建
- svn環境搭建
- centos lamp安裝配置
- mysql
- mysql常用命令
- mysql技術內幕
- 1.1mysql體系結構
- 1.2mysql存儲引擎
- 1.3mysql連接
- linux
- linux-常用命令
- linux下vim命令
- 第三方平臺開發
- 微信開發之旅
- php
- php框架
- lavarel常用命令
- thinkPhp常用命令
- yii2.0.8
- 安裝
- yii常用
- yii配置
- yii常用2
- php源碼積累
- php字符串截取
- php圖片處理(gd)
- 二維數組保持索引排序(高低)
- 獲取一個月首尾天數
- 時間函數
- php內置函數
- html
- js
- 基本命令
- js案例
- js去空格
- css
- 基本樣式
- 案例
- ul li 橫向水平居中自適應案例
- 固定底部導航欄并自適應
- 購物車帶角標
- display的兼容解決
- 前端框架
- boostrap
- 常用類
- git
- 上傳項目到遠程倉庫GitHub