Last active
August 29, 2018 09:06
-
-
Save liangzai-cool/b28e76b534ccbf80165377ed512fb222 to your computer and use it in GitHub Desktop.
更新数据库树结构的level和code列
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 初始化 | |
update tjyqhdmhcxhfdm set level = -1, code = ''; | |
update tjyqhdmhcxhfdm set level = 0, code = concat(parentId, ',', id, ',') where parentId = 0; | |
-- 查询 | |
select * from tjyqhdmhcxhfdm_2017 where level >= 0 order by level asc, parentId asc, code asc limit 1000; | |
-- 更新 | |
update | |
tjyqhdmhcxhfdm t1 inner join tjyqhdmhcxhfdm_2017 t2 on t1.parentId = t2.id | |
set | |
t1.level = t2.level + 1, | |
t1.code = concat(t2.code, t1.id, ',') | |
where t2.level = 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment