记录一下mysql中使用Update from 语句更新字段值:
场景是国家行政区划的数据表,字段内容包括:
应用场景中不想使用级联选择,选中下级节点,比如东城区时,直接显示北京-北京市-东城区,因此打算给数据表增加一个字段fullname, 显示完整名称:
--更新第一级省的fullnameupdate tb_conf_area set fullname=name where LEVEL =1--更新第二级市的fullnameupdate tb_conf_area a inner join (select a.*, concat(b.fullname,'-',a.name) as parentname from tb_conf_area a inner join tb_conf_area b on a.pid=b.oidwhere a.level=2) bon a.oid=b.OID set a.fullname=b.parentnamewhere a.`LEVEL` =2--更新第三级区的fullnameupdate tb_conf_area a inner join (select a.*, concat(b.fullname,'-',a.name) as parentname from tb_conf_area a inner join tb_conf_area b on a.pid=b.oidwhere a.level=3) bon a.oid=b.OID set a.fullname=b.parentnamewhere a.`LEVEL` =3--更新第四级街道的fullnameupdate tb_conf_area a inner join (select a.*, concat(b.fullname,'-',a.name) as parentname from tb_conf_area a inner join tb_conf_area b on a.pid=b.oidwhere a.level=4) bon a.oid=b.OID set a.fullname=b.parentnamewhere a.`LEVEL` =4
来源地址:https://blog.csdn.net/xianglayu/article/details/127512455