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.'条记录。。。' : '没有记录被删除';
}
}