Blogger Information
Blog 14
fans 0
comment 0
visits 8627
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
查询构造器最常用的10个方法 2018.5.24 16:53
弗洛加特的博客
Original
777 people have browsed it

namespace app\index\controller;
use think\Db;

class Query
{
   public function find()
   {
       $res = Db::table('staff')
//             ->field('name,age,salary')
               ->field(['name'=>'姓名','age'=>'年龄','salary'=>'工资'])
//             ->where('staff_id','>',10)
//             ->where('staff_id > 10')
            ->find(10);
       dump($res);
   }

   public function select()
   {
       $res = Db::table('staff')
           ->field(['name'=>'姓名','age'=>'年龄','salary'=>'工资'])
           ->where('salary','>',3000)
//            ->order('salary desc')
           ->order('salary','desc')
           ->limit(5)
           ->select();
       dump($res);
   }

   public function insert()
   {
       //新增单条记录
       $data=[
           'name'=>'胡一刀',
           'sex'=>0,
           'age'=>49,
           'salary'=>5000
       ];
//        $num = Db::table('staff')->insert($data);
//        $num = Db::table('staff')->data($data)->insert();
//        $id = Db::getLastInsID();
//        echo $num ? '新增成功,id='.$id : '没有记录被添加';

       $id = Db::table('staff')->insertGetId($data);
       echo $id ? '新增成功,id='.$id : '没有记录被添加';
      
   }

public function insertAll()
{
   $data =[
       ['name'=>'张飞','sex'=>0,'age'=>43,'salary'=>4300],
       ['name'=>'关羽','sex'=>0,'age'=>48,'salary'=>4800],
       ['name'=>'刘备','sex'=>0,'age'=>60,'salary'=>6000]
   ];
   $num = Db::table('staff')->data($data)->insertAll();
   echo $num ? '新增成功,num='.$num.'条记录。。。' : '没有记录被添加';

}

public function update()
   {
//        $num = Db::table('staff')
//            ->where('salary <= 3000')
//            ->data(['salary' => Db::raw('salary+1000')])
//            ->update();
       $num = Db::table('staff')->update(['salary'=>6000,'staff_id'=>1]);
       echo $num ? '更新成功'.$num.'条记录。。。' : '没有记录被更新';

   }

public function delete()
{
   $num = Db::table('staff')->delete(21);
   $num = Db::table('staff')->delete([23,24,25]);
   $num = Db::table('staff')
       ->where('salary','>',10000)
       ->delete();
   $num = Db::table('staff')->delete(true);  //删除整张staff表

   echo $num ? '删除成功'.$num.'条记录。。。' : '没有记录被删除';

}



}

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post