《Laravel5.2学习笔记---数据库操作之查询构造器》

  1. Laravel查询构造器(query builder)提供方便、流畅的接口,用来建立及执行数据库查找语法
  2. 使用PDO参数绑定,以保护应用程序免于SQL注入,因此传入的参数不需要额外转义特殊字符
  3. 基本可以满足所有的数据库操作,而且在所支持的数据库系统上都可以执行。
StudentController.php
<?php
namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
class StudentController extends Controller{
    public function query1(){

        //插入数据

        // $bool = DB::table('student')->insert(
        //  ['name' => 'chen', 'age' => 18]
        // );
        // var_dump($bool);

        //新增数据时获取自增ID

        // $id = DB::table('student')->insertGetId(
        //  ['name'=>'xing', 'age' => 18]
        // );
        // var_dump($id);

        //插入多条数据

        // $bool = DB::table('student')->insert([
        //  ['name' => 'test1', 'age' => 11],
        //  ['name' => 'test2', 'age' => 12],
        //  ['name' => 'test3', 'age' => 13],
        // ]);
        // var_dump($bool);
    }

    public function query2(){
        //更新数据
        // $num = DB::table('student')
        // ->where('id',1006)
        // ->update(['age'=>30]);
        // var_dump($num);
        //自增自减
        //$num = DB::table('student')->increment('age',3);//age字段都自增3,不写那个3,默认是自增1
        //如果自减,就将increment改成decrement

        //带条件的自增自减
        // $num = DB::table('student')
        // ->where('id', 1006)
        // ->increment('age',3);
        // var_dump($num);

        //如果想在自增的时候再修改其它字段
        // $num = DB::table('student')
        // ->where('id', 1006)
        // ->increment('age', 3, ['name'=>'yyyyy']);
        // var_dump($num);
    }

    public function query3(){
        //删除数据
        // $num = DB::table('student')
        // ->where('id', '>=', 1004)
        // ->delete();
        // var_dump($num);

        //整表删除(一般不要使用它)
        //DB::table('student')->truncate();//它不返回任何东西
    }

    public function query4(){
        //查询数据
        //get()获取所有记录
        // $student = Db::table('student')->get();
        // dd($student);

        //first()返回第一条记录
        // $stu = DB::table('student')
        // ->orderBy('id', 'desc')
        // ->first();
        // dd($stu);

        //where()
        // $stu = DB::table('student')
        // ->where('id', '>=', 2)
        // ->get();
        // dd($stu);
        //添加多个条件
        // $stu = DB::table('student')
        // ->whereRaw('id>=? and age>?', [3, 14])
        // ->get();

        //pluck()返回结果集中指定的字段
        // $stu = DB::table('student')
        // ->whereRaw('id>=? and age>?', [3, 14])
        // ->pluck('name');//只要name字段
        // dd($stu);

        //lists()它和pluck()函数的功能差不多,不同的是,它可以指定哪个字段作为它的下表
        // $stu = DB::table('student')
        // ->whereRaw('id>=? and age>?', [3, 14])
        // ->lists('name', 'id');//只要name字段
        // dd($stu);

        //select()查询指定字段
        // $stu = DB::table('student')
        // ->select('name', 'age')
        // ->get();

        //chunk()如果我们有几十万条数据,我们直接使用get(),服务器肯定就爆了,使用chunk()我们就可以分段获取
        // DB::table('student')->chunk(2, function($student){//表示每次查询两条
        //  var_dump($student);
        //  //我们还可以在里边加条件语句,让其指定条件下停止查询
        //  return false;
        // });
    }
    public function query5(){
        //聚合函数
        // $num = DB::table('student')->count();//返回记录数
        // var_dump($num);
        // $max = DB::table('student')->max('age');//获取最大年龄
        // $min = DB::table('student')->min('id');//获取最小id
        // $avg = DB::table('student')->avg('age');//获取平均年纪
        // $sum = DB::table('student')->sum('age');//年纪总和
        // echo $max,$min,$avg,$sum;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值