前言[随时更新]
MySQL快速复习
MySQL原生查询
选择数据表
删除数据
聚合查询
时间查询
视图查询
子查询
总结/参考
官方手册:只提供一个更新一个字段的方法
查看源码发现,其实将参数换成数组,可以同时更新多个字段

| 序号 | 输入参数 | 返回值 |
|---|---|---|
| 1 | 2个参数时('字段名','字段值') 1个参数时(与字段名对应的数组) |
受影响记录条数 即更新数量 |
以单条记录为例,下面给出常用的四种用法:
Db::table( 完整表名) -> where(更新条件) -> setField('字段名' , '字段值');
// 例如:
Db::table('tp5_staff') -> where('id',1024) -> setField('salary' , 8500);
// 1.创建员工信息数组 $data = []; $data[id] = 主键值; //设置更新条件 $data[ 字段1 ] = 字段值1 ; $data[ 字段2 ] = 字段值2 ; ...... //执行更新操作 Db::table( 表名 ) -> setField($data);
// 1.创建员工信息数组 $data = []; $data[ 字段1 ] = 字段值1 ; $data[ 字段2 ] = 字段值2 ; ...... //执行更新操作 Db::table( 表名 ) -> where( 更新条件 ) -> setField($data);
//将更新条件与数据一次性传入 Db::table( 表名 )->setField(['主键字段'=> 主键值,'字段1'=>字段值1,'字段2'=>字段值2,...]); //如果更新主键不放在参数数组中,则在方法前添加where方法 Db::table( 表名 )->where( 更新条件 ) -> setField(['字段1'=>字段值1,'字段2'=>字段值2,...]);
手册上,并没有给出同时更新多条记录的方法,其实更新多条记录也很简单,只要在更新方法前,设置好更新条件即可。
//如果更新主键不放在参数数组中,则在方法前添加where方法
Db::table( 表名 )->where( 更新条件 ) -> setField(['字段1'=>字段值1,'字段2'=>字段值2,...]);
//例如,将表中员工的工资小于3000元的,加薪500元,福利好吧?
Db::table('tp5_staff' )->where( 'salary < 3000' ) -> setField( [ 'salary' => [ 'exp', 'salary + 500' ] ] );
可能有同学对: [ 'salary' => [ 'exp', 'salary + 500' ] ] ) 写法不能理解,随着后面对查询表达式的学习,就理解了,这里仅仅知道可以同时更新多条记录即可。
先查看一下当前tp5_staff表中数据

<?php
namespace app\index\controller;
use think\Db;
class Index {
public function index(){
// 1.更新数据
Db::table('tp5_staff') -> where('id',1007) -> setField('salary',8567);
// 2.查看更新结果
dump(Db::table('tp5_staff')->find(1007));
}
}
array(7) {
["id"] => int(1007)
["name"] => string(9) "潘金莲"
["sex"] => int(0)
["age"] => int(39)
["salary"] => float(8567)
["dept"] => int(3)
["hiredate"] => string(10) "2016-03-20"
}
<?php
namespace app\index\controller;
use think\Db;
class Index {
public function index(){
dump(Db::table('tp5_staff')->find(1011));
}
}
array(7) {
["id"] => int(1011)
["name"] => string(9) "李云龙"
["sex"] => int(1)
["age"] => int(39)
["salary"] => float(4800)
["dept"] => int(1)
["hiredate"] => string(10) "2011-09-12"
}
<?php
namespace app\index\controller;
use think\Db;
class Index {
public function index(){
//1.创建要更新的数据
$data = [];
$data['id'] = 1011;
$data['age'] = 49;
$data['salary'] = 9850;
$data['hiredate'] = '2005-10-20';
// 2.更新数据
Db::table('tp5_staff') -> setField($data);
// 3.查看更新结果
dump(Db::table('tp5_staff')->find(1011));
}
}
array(7) {
["id"] => int(1011)
["name"] => string(9) "李云龙"
["sex"] => int(1)
["age"] => int(49)
["salary"] => float(9850)
["dept"] => int(1)
["hiredate"] => string(10) "2005-10-20"
}

注意:该操作涉及多记录更新,与前面实例不同

<?php
namespace app\index\controller;
use think\Db;
class Index {
public function index(){
//1.创建要更新条件
$map['age'] = ['between',[20,30]];
//2.创建更新数据
$data = [];
$data['dept'] = 3;
$data['salary'] = ['exp','salary + 1000'];
$data['hiredate'] = '2012-12-12';
// 2.更新数据
Db::table('tp5_staff') -> where($map) -> setField($data);
// 3.查看更新结果
dump(Db::table('tp5_staff')->where($map)->order('age')->select());
}
}
array(4) {
[0] => array(7) {
["id"] => int(1001)
["name"] => string(6) "郭靖"
["sex"] => int(0)
["age"] => int(22)
["salary"] => float(6679)
["dept"] => int(3)
["hiredate"] => string(10) "2012-12-12"
}
[1] => array(7) {
["id"] => int(1025)
["name"] => string(9) "小铃铛"
["sex"] => int(0)
["age"] => int(22)
["salary"] => float(6739)
["dept"] => int(3)
["hiredate"] => string(10) "2012-12-12"
}
[2] => array(7) {
["id"] => int(1020)
["name"] => string(6) "虚竹"
["sex"] => int(0)
["age"] => int(28)
["salary"] => float(5765)
["dept"] => int(3)
["hiredate"] => string(10) "2012-12-12"
}
[3] => array(7) {
["id"] => int(1002)
["name"] => string(9) "洪七公"
["sex"] => int(0)
["age"] => int(29)
["salary"] => float(5365)
["dept"] => int(3)
["hiredate"] => string(10) "2012-12-12"
}
}
1.update方法与setField方法都可以完成同样的工作;
2.日常开发中,推荐使用update方法;
3.当仅更新单条记录中某个字段值时,用setField方法更简洁和直观。
setField方法的若干用法,建议每个都上机操作一遍~~