当前位置:网站首页>MySQL根据查询的数据更新记录

MySQL根据查询的数据更新记录

2022-06-26 01:34:00 哈希疯

更新行政区域的父级行政区代码

UPDATE system_region,(
	SELECT
		system_region.id,
		system_region.`code`,
		system_region.full_name,
		temp1.full_name province,
		temp2.full_name city,
		temp1.`code` provinceCode,
		temp2.`code` cityCode
	FROM
		system_region
		LEFT JOIN system_region temp1 ON temp1.`code` = CONCAT(LEFT(system_region.`code`,2),'0000') AND system_region.`code` != CONCAT(LEFT(system_region.`code`,2),'0000')
		LEFT JOIN system_region temp2 ON temp2.`code` = CONCAT(LEFT(system_region.`code`,4),'00') AND system_region.`code` != CONCAT(LEFT(system_region.`code`,4),'00')
) t
SET
	parent_code = IF(t.cityCode, t.cityCode, t.provinceCode)
WHERE
	system_region.id = t.id
原网站

版权声明
本文为[哈希疯]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yu57955/article/details/125263102