Skip to content

Instantly share code, notes, and snippets.

@liangzai-cool
Last active August 29, 2018 09:06
Show Gist options
  • Save liangzai-cool/b28e76b534ccbf80165377ed512fb222 to your computer and use it in GitHub Desktop.
Save liangzai-cool/b28e76b534ccbf80165377ed512fb222 to your computer and use it in GitHub Desktop.
更新数据库树结构的level和code列
-- 初始化
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