请教mysql批量更新层级关系数据如何实现
有个地址表(数据量较大,百万级的) ,只给3个主要字段:地址id,地址等级,上级地址id:
select id,grade,parentid from addr
where id in (512589,106969,43072,281,7651,200087792,153,103,1)
;
ID GRADE PARENTID
1 1 1 0
2 103 2 1
3 153 3 103
4 281 5 7651
5 7651 5 200087792
6 43072 6 281
7 106969 7 43072
8 512589 8 106969
9 200087792 4 153
现在有个临时表 (10w级数据)
select stid, addr6 from tmp_addr;
需求是根据tmp_addr表的stid来更新所有记录的6级地址,
在oracle里可以这样实现:
update tmp_addr xx
set xx.addr6=(select id from addr where grade=6 and rownum=1 start with id=xx.stid connect by nocycle prior parentid=id)
;
请教下大家如何在mysql里实现这样的功能,我在网上搜了下很多是通过函数传一个参数id,然后用find_in_set,将所有父节点拼接起来,
但是这种每次只能查询一条记录的,我在存储过程里用游标循环试了下,实在是太慢了,tmp_addr 1w多记录就跑了1个多小时还没跑完,就放弃这种方法了,
大家有好的方法或方法,请不吝赐教,谢谢!