<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] # 基本用法 注意這邊from后面要跟完整的表名,就算.env文件配置過了也不行 ~~~ //DB命名空間是use Illuminate\Support\Facades\DB; //查詢 $result=DB::select('select * from blog_stu'); //增加 $result=DB::insert('insert into blog_stu(name,age) values(?,?)',['sean',18]); //更新 $result=DB::update('update blog_stu set age=? where name=?',[21,'sean']); //查詢 $result=DB::select('select * from blog_stu where id>?',[2]); //刪除 $result=DB::delete('delete from blog_stu where id>?',[3]); //添加一條數據并獲取自動遞增的id號 $id = DB::table('users')->insertGetId(['email'=>'shuoshuo', 'pass'=>'2121']); ~~~ ![](https://box.kancloud.cn/aa984fb3ec658367c286579de78c2a79_1162x826.png) ~~~ first 方法是取得結果集數組中第一列數據,如果結果集為空則返回 null 。 pluck 方法是取得結果集第一列特定字段,它返回是字符串; lists 方法是按照 key=>value 對的方式返回數組;它的參數最多兩個,第一個參數作為鍵值(value),第二個參數作為鍵名(key) ~~~ # 條件子句 有時我們只想在某些條件成立的時候呢才去執行一些條件子句查詢。比如,對于 where 子句,我們只想在輸入數據里包含某個字段的時候,才去執行,那么這時就要用到 when 了。 ~~~ $role = $request->input('role'); $users = DB::table('users') ->when($role, function ($query) use ($role) { return $query->where('role_id', $role); }) ->get(); ~~~ 在上面這段代碼里,只有當閉包的第一個參數($role)判定為 true 時才會執行。如果第一個參數的判定值是 false,那么就不會執行閉包里的內容。 when 方法還接受第三個參數,跟第二個參數一樣,也是一個閉包——當判定條件(第一個參數)為 false 時,就會執行這里的邏輯。為了說明這個方法的使用場景,我們來舉一個配置默認使用的排序字段的例子: ~~~ $sortBy = null; $users = DB::table('users') ->when($sortBy, function ($query) use ($sortBy) { return $query->orderBy($sortBy); }, function ($query) { return $query->orderBy('name'); }) ->get(); ~~~ 當你在使用where語句有前提條件時,比如某值為1的時候才執行where子句,否則不執行,這個時候,laravel5.5新出了一個簡便方法when($arg,fun1\[,fun2\])。 具體用法如下:當$arg為真時,執行閉包fun1,為假時,執行閉包fun2(可選); ~~~ when($arg,function ($q){ return $q->orderBy('id', 'asc'); }, function ($q) use ($a){ return $q->orderBy($a, 'desc'); }); ~~~ 當$arg為真是,執行按id升序排序,當$a為假時,執行按$a降序排序 # 查詢構造器 不要寫完整表名了,完整表名的blog_stu,然后.env文件里面配置過表名的前綴了 ~~~ //增加(返回主鍵id) $bool=DB::table('stu')->insertGetId( ['name'=>'yabo','age'=>23] ); //當前插入的主鍵id $bool=DB::table('stu')->insert( ['name'=>'yaboo','age'=>23] ); //成功返回true $nm=DB::table('stu')->insert([ ['name'=>'name1','age'=>17], ['name'=>'name2','age'=>18], ['name'=>'name3','age'=>19], ]); ~~~ ~~~ //更新 $bool=DB::table('stu')->where('id',8) ->update(['age'=>20]); //成功返回1 ~~~ ~~~ //刪除(切記帶where條件) $num=DB::table('stu')->where('id',8) ->delete(); //成功返回1 //如果要清空一個表可以這樣,但是很危險 $nm=DB::table('stu')->truncate(); //null 成功了 ~~~ # 自增自減 ~~~ //數據庫一共5條數據,全部把年齡自增1 $bool=DB::table('stu')->increment('age');//返回5 //上面代碼想自增3 $bool=DB::table('stu')->increment('age',3);//返回5 //上面代碼想自減3 $bool=DB::table('stu')->decrement('age',3);//返回5 //加條件 $bool=DB::table('stu')->where('id',6)->decrement('age',3,['name'=>'jdxia']);//返回1 ~~~ # 查詢數據 ~~~ //get獲取的意思,這是獲取所有的數據 $nm=DB::table('stu')->get(); //取出第一條數據 $nm=DB::table('stu')->orderBy('id','desc')->first(); //where(單個條件) $nm=DB::table('stu')->where('id','>=',3)->get(); //where(給where加多個條件) $nm=DB::table('stu')->whereRaw('id>=? and age>?',[3,19])->get(); //pluck返回結果集指定的字段,返回只有name字段 $nm=DB::table('stu')->pluck('name'); //lists(上面的pluck返回結果加上以...為指定下標) $nm=DB::table('stu')->lists('name','id'); //select (查詢的時候不想查詢出所有的數據只想查詢出指定的字段) $nm=DB::table('stu')->select('id','name')->get(); //chunk 每次查詢2條,依次查詢出來,或者手動return $nm=DB::table('stu')->chunk(2,function($students){ print_r($students); //if(你的條件) return false; }); ~~~ # 聚合函數 ~~~ //統計 $nm=DB::table('stu')->count(); //最大值 $nm=DB::table('stu')->max('age'); //最小值 $nm=DB::table('stu')->min('age'); //平均值 $nm=DB::table('stu')->avg('age'); //和 $nm=DB::table('stu')->sum('age'); ~~~ # 連接查詢 ## 內連接(等值連接) 查詢構建器還可以用于編寫基本的 SQL “內連接”,你可以使用查詢構建器實例上的 join 方法,傳遞給 join 方法的第一個參數是你需要連接到的表名,剩余的其它參數則是為連接指定的列約束,當然,正如你所看到的,你可以在單個查詢中連接多張表: ~~~ $users = DB::table('users') ->join('contacts', 'users.id', '=', 'contacts.user_id') ->join('orders', 'users.id', '=', 'orders.user_id') ->select('users.*', 'contacts.phone', 'orders.price') ->get(); ~~~ ## 左連接 如果你是想要執行“左連接”而不是“內連接”,可以使用 leftJoin 方法。該方法和 join 方法的用法一樣: ~~~ $users = DB::table('users') ->leftJoin('posts', 'users.id', '=', 'posts.user_id') ->get(); ~~~ ## 交叉連接 要執行“交叉連接”可以使用 crossJoin 方法,傳遞你想要交叉連接的表名到該方法即可。交叉連接在第一張表和被連接表之間生成一個笛卡爾積: ~~~ $users = DB::table('sizes') ->crossJoin('colours') ->get(); ~~~ ## 聯合(union) 查詢構建器還提供了“聯合”兩個查詢的快捷方式,比如,你可以先創建一個查詢,然后使用 union 方法將其和第二個查詢進行聯合: ~~~ $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(); ~~~ 還有一個 lockForUpdate 方法,比 sharedLock 還厲害——可以保證用戶 在讀取數據時,保證數據不被修改或被另一個共享鎖所選擇。 ~~~ DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get(); ~~~ ## 其他常見用法 latest / oldest ``` latest / oldest 方法實現字段按照日期便捷地排序。默認是依據 created_at 字段排序的,你也可以傳遞要排序的字段名以便覆蓋默認設定: $user = DB::table('users') ->latest() ->first(); $user = DB::table('users') ->latest('updated_at') ->first(); ``` groupBy / having / havingRaw groupBy 和 having 方法可以用來給查詢結果進行分組。having 方法的簽名類似 where 方法: ``` $users = DB::table('users') ->groupBy('account_id') ->having('account_id', '>', 100) ->get(); ``` havingRaw 方法用來設置原生字符串到 having 子句中。 例如,我們可以找到所有銷售額大于 2500刀 的部門: ``` $users = DB::table('orders') ->select('department', DB::raw('SUM(price) as total_sales')) ->groupBy('department') ->havingRaw('SUM(price) > 2500') ->get(); ``` ## JSON Where 子句 Laravel 也支持數據庫 JSON 字段類型的查詢,前提是數據庫支持 JSON 字段類型。現在 MySQL 5.7 和 Postgres 都支持。查詢 JSON 字段,使用 -> 操作符: ~~~ $users = DB::table('users') ->where('options->language', 'en') ->get(); $users = DB::table('users') ->where('preferences->dining->meal', 'salad') ->get(); ~~~ ## 日期where whereDate / whereMonth / whereDay / whereYear whereDate 用來比較字段值是否滿足給定的日期。 ~~~ $users = DB::table('users') ->whereDate('created_at', '2016-12-31') ->get(); ~~~ whereMonth 用來比較字段值是否滿足給定的月份。 ~~~ $users = DB::table('users') ->whereMonth('created_at', '12') ->get(); ~~~ whereDay 用來比較字段值是否滿足給定的日期。 ~~~ $users = DB::table('users') ->whereDay('created_at', '31') ->get(); ~~~ whereYear 用來比較字段值是否滿足給定的年份。 ~~~ $users = DB::table('users') ->whereYear('created_at', '2016') ->get(); ~~~ whereBetween限制范圍 ~~~ whereBetween('created_at',[date('Y-m-d').' 00:00:00',date('Y-m-d').' 23:59:59']) ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看