1、查询一条记录
// 链式查询public function types(){$res = DB::select('select username,ispasswd from admin where id=12');$res2 = DB::table('admin')->where('id',12)->first(['username','ispasswd']);echo '<pre>';print_r($res);print_r($res2);}

2、查询多条记录
// 查询多条记录public function lists(){$res = DB::table('admin')->where('ispasswd',2)->get(['username','real_name']);echo '<pre>';print_r($res);}

// 查询多条记录public function lists(){$res = DB::table('admin')->where('ispasswd',2)->get(['username','real_name']);$data = $res->toArray();echo '<pre>';print_r($data);}

3、查询某一列的当前值
// 查询某一列的当前值public function val(){$res = DB::table('admin')->where('id',7)->first(['username']);$res2 = DB::table('admin')->where('id',7)->value('username');echo '<pre>';print_r($res);echo '<hr>';print_r($res2);}

4、获取一列值
$res2 = DB::table('admin')->pluck('username');

// 链式新增记录public function inserts2(){$data = ['username'=>'xiaohu','password'=>'asdfasdfasdfasdf','real_name'=>'胡总'];$res = DB::table('admin')->insert($data);// $res2 = DB::table('admin')->insertGetId($data);var_dump($res);}

// 链式更新记录public function update2(){$res = DB::table('admin')->where('id',34)->update(['ispasswd'=>2]);var_dump($res);}

// 链式删除记录public function delete2(){$res = DB::table('admin')->where('id',34)->delete();var_dump($res);}

// 聚合查询public function juhe(){// gid最大值$res = DB::table('admin')->max('gid');// gid最小值$res2 = DB::table('admin')->min('gid');// 平均值$res3 = DB::table('admin')->avg('gid');// 求和$res4 = DB::table('admin')->sum('gid');// 记录总数$res5 = DB::table('admin')->where('gid',0)->count();var_dump($res);echo '<hr>';var_dump($res2);echo '<hr>';var_dump($res3);echo '<hr>';var_dump($res4);echo '<hr>';var_dump($res5);}


// 更改字段名public function change(){// 原生$res = DB::select('select username as yonghuming from admin');// 链式$res2 = DB::table('admin')->select('id','username as yonghuming','real_name')->get();echo '<pre>';print_r($res2);}

// where 方法public function where(){$res = DB::table('admin')->where('gid','<>',1)->get();echo '<pre>';print_r($res);}
// betweenpublic function between(){$res = DB::table('admin')->whereBetween('gid',[1,10])->get(['username','gid']);echo '<pre>';print_r($res);}

// whereInpublic function wherein(){$res = DB::table('admin')->whereIn('id',[1,18,32])->get(['id','username','real_name']);echo '<pre>';print_r($res);}

// or查询public function orwhere(){$res = DB::table('admin')->where('gid',0)->orWhere('ispasswd',1)->get(['gid','ispasswd','username']);echo '<pre>';print_r($res);}

public function orwhere(){$res = DB::table('admin')->where('gid',0)->orWhere('ispasswd',1)->get(['id','username','real_name']);echo '<pre>';print_r($res->toArray());print_r($res->all());}

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号