博客列表 >综合实战第四课:laravel基础3-PHP培训九期线上班

综合实战第四课:laravel基础3-PHP培训九期线上班

渡劫小能手
渡劫小能手原创
2020年01月03日 00:29:43618浏览

数据库查询

where

对象变数组

  1. echo '<pre>';
  2. $res = DB::table('product')->where('id',2)->get()->toArray();
  3. foreach ($res as $item){
  4. $item = (array)$item;
  5. print_r($item);
  6. }

一维数组

  1. echo '<pre>';
  2. $res = DB::table('product')->where('id',2)->get()->toArray();
  3. foreach ($res as $key => $item){
  4. $res[$key] = (array)$item;
  5. }
  6. print_r($res);

二维数组

whereBetween

区间查询,id在4和6之间的,包括4和6

  1. echo '<pre>';
  2. $res = DB::table('product')->whereBetween('id',[4,6])->get()->toArray();
  3. foreach ($res as $key => $item){
  4. $res[$key] = (array)$item;
  5. }
  6. print_r($res);

whereIn

数据库查询尽量用in查询,杜绝用or,严重影响性能。in里面也不能太多

  1. echo '<pre>';
  2. $res = DB::table('product')->whereIn('id', [4, 5, 6, 7])->get()->toArray();
  3. foreach ($res as $key => $item) {
  4. $res[$key] = (array)$item;
  5. }
  6. print_r($res);

orWhere

  1. echo '<pre>';
  2. $res = DB::table('product')->orWhere('id','=',3)->orWhere('id','=',5)->get()->toArray();
  3. foreach ($res as $key => $item) {
  4. $res[$key] = (array)$item;
  5. }
  6. print_r($res);

指定字段select

  1. $res = DB::table('product')->select('id','pname','price')->whereIn('id',[3,4,5,7,9])->get()->toArray();
  2. foreach ($res as $key => $item) {
  3. $res[$key] = (array)$item;
  4. }
  5. print_r($res);

聚合

count

  1. $res = DB::table('product')->count();
  2. print_r($res);

max

  1. $res = DB::table('product')->max('price');
  2. print_r($res);

min

  1. $res = DB::table('product')->min('price');
  2. print_r($res);

avg

求平均值需要扫描表,对性能造成很大的影响

  1. $res = DB::table('product')->avg('price');
  2. print_r($res);

sum

leftJoin

对数据库性能影响很大,包括join,rightjoin,尽量优化掉

  1. $res = DB::table('product')->leftJoin('category','product.category_id','=','category.cid')->select('product.id','product.pname','product.price','category.cname','product.category_id as productid','category.cid as cateid' )->get()->toArray();
  2. foreach ($res as $key => $item) {
  3. $res[$key] = (array)$item;
  4. }
  5. print_r($res);

化解leftJoin

方式一

有个foreach循环,采用插入数组新字段的方法化解,频繁查询数据库,并不完美

  1. $res = DB::table('product')->get()->toArray();
  2. // 拿出product的所有数据
  3. foreach ($res as $key => $val){
  4. $cate = DB::table('category')->where('cid',$val->category_id)->first();
  5. // 循环category表中cid=product中category_id的数据,每次一条
  6. $res[$key]->cate_name = $cate->cname;
  7. // 在product数据中强行插入一个key为cate_name的字段,值为上面的循环
  8. }
  9. foreach ($res as $key => $item) {
  10. $res[$key] = (array)$item;
  11. }
  12. return view('product.lists',['products'=>$res]);
  1. <table>
  2. <thead>
  3. <tr>
  4. <th>cname</th>
  5. <th>pname</th>
  6. <th>price</th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. @foreach($products as $product)
  11. <tr>
  12. <td>{{$product['cname']}}</td>
  13. <td>{{$product['pname']}}</td>
  14. <td>{{$product['price']}}</td>
  15. </tr>
  16. @endforeach
  17. </tbody>
  18. </table>
方式二

采用返回给view2个结果集,结果集互相调用

  1. $res = DB::table('product')->get()->toArray();
  2. $cname = DB::table('category')->get()->toArray();
  3. $cates = [];
  4. // 定义一个新的数组
  5. foreach ($cname as $val){
  6. $cates[$val->cid] = $val->cname;
  7. // 构造一个一维维数组,下标就是cid,值是cname
  8. // $cates[$val->cid]['cname'] = $val->cname;
  9. // 构造一个二维维数组
  10. }
  11. foreach ($res as $key => $item) {
  12. $res[$key] = (array)$item;
  13. }
  14. return view('product.lists',['products'=>$res],['cates'=>$cates]);
  1. <table>
  2. <thead>
  3. <tr>
  4. <th>cname</th>
  5. <th>pname</th>
  6. <th>price</th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. @foreach($products as $product)
  11. <tr>
  12. <td>{{$cates[$product['category_id']]}}</td>
  13. // <td>{{$cates[$product['category_id']]['cname']}}</td>
  14. <td>{{$product['pname']}}</td>
  15. <td>{{$product['price']}}</td>
  16. </tr>
  17. @endforeach
  18. </tbody>
  19. </table>

数据库插入

  1. DB::table('category')->insert(['cid'=>4,'cname'=>'电脑']);

insertGetId

如果数据表有自增 ID ,使用 insertGetId 方法来插入记录并返回 ID 值

  1. $res = DB::table('category')->insertGetId(['cid'=>5,'cname'=>'水果']);
  2. var_dump($res);

数据库更新

  1. $res = DB::table('category')->where('cid',4)->update(['cname'=>'笔记本电脑']);
  2. var_dump($res);

数据库删除

  1. $res = DB::table('category')->where('cid',4)->delete();
  2. var_dump($res);
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议