Blogger Information
Blog 34
fans 0
comment 0
visits 28529
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
523以实例演示:查询构造器中的10个最常用的方法
1A7498的博客
Original
906 people have browsed it
<?php
namespace app\index\controller;
use think\Db;

class Query
{
    //读操作返回的都是二维数组,没有就是空数组
    //写操作返回的是备操作的技术数量
    public function find()
    {
        //查询单挑记录
        $res = Db::table('staff')
            ->field(['name'=>'姓名','sex'=>'性别','salary'=>'工资'])
            ->where('staff_id',10)
            ->find();
        dump($res);
    }
    public function select()
    {
        //查询多条记录
        $res = Db::table('staff')
            ->field(['name'=>'姓名','sex'=>'性别','salary'=>'工资','staff_id'=>'id'])
            ->where('salary > 5000')
            ->order('salary DESC')//排序,从大到小
            ->limit(2)
            ->select();
        dump($res);
    }
    public function insert()
    {
        //新增单挑记录
//        $data = [
//            'name'=>'9529',
//            'sex'=>0,
//            'age'=>22,
//            'salary'=> 4568
//        ];
//        $num = Db::table('staff')->insert($data);
//        $id = Db::getLastInsID();
//        return $num ?'添加成功,id='.$id :'没记录';


//        $id = Db::table('staff')->insertGetId($data);
//        return $id ?'添加成功,id='.$id :'没记录';
//
//        $num = Db::table('staff')->($data)->insert();
//        $id = Db::getLastInsID();
//        return $num ?'添加成功,id='.$id :'没记录';

        $data = [
            ['name'=>'9529','sex'=>0,'age'=>22,'salary'=> 784512],
            ['name'=>'9530','sex'=>0,'age'=>22,'salary'=> 74125],
            ['name'=>'9531','sex'=>0,'age'=>22,'salary'=> 85268],
            ['name'=>'9532','sex'=>0,'age'=>22,'salary'=> 85214]
        ];
        $num = Db::table('staff')->data($data)->insertAll();
        return $num? '添加成功'.$num.'个记录':'no';
    }
    public function updata()
    {
        //更新操作必须是基于前置查询,不允许无条件更新
        //判断工资大于9999的下降1万元
//        $num = Db::table('staff')
//            ->where('salary','>',9999)
//            ->data(['salary' => Db::raw('salary-10000')])
//            ->update();
        $num = Db::table('staff')
            ->update(['sex'=>1,'staff_id'=>26]);
        return $num? '更新成功'.$num.'个记录':'no';

    }
    public function delete()
    {
        //删除是基于前置查询,不允许无条件删除
        $num = Db::table('staff')
            ->where('salary','>',10000)
            ->delete();
        //$num = Db::table('staff')->delete(ture);//清空表
        return $num? '删除成功'.$num.'个记录':'no';
    }
}

QQ截图20180525135856.png


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
  • 2018-03-16 11:39:01