有个数据表post字段如下:保存类似于树的结构
post_id    主键,用来表示数据节点id,自动增加
g_id       数据所在组id
title      标题   
parent_id  父节点id(无父节点则为0,否则为其父数据的post_id)
root_id    根节点id(无父节点则为自己的post_id,否则等于其根节点的post_id)
last_id    最末子节点id(若节点为根节点,last_id等于自己的post_id,否则为0)表detail字段如下:
post_id     主键,表示数据节点id
detail      详细信息存储过程:每个post的数据,都会对应detail中有一条数据,通过post_id关联,
我想把对应post.post_id=etail.post_id=100的所有数据都分别复制到新表同样结构的post_new和detail_new中去问题是,我不想复制原有post_id的这个值,我想post_new和detail_new的post_id自己产生,可是由于parent_id,root_id以及last_id和post_id有关联该如何做呢,这个是不是树的重构问题?100分高分求助,能给我解释清楚,愿意散更多的分
谢谢先
例如 post 表数据 如下
+----------+----------+-----------+----------+----------+----+
| post_id  | board_id | parent_id | root_id  | last_id  |    |
+----------+----------+-----------+----------+----------+----+
| 11026347 |      110 |         0 | 11026347 | 11030832 |    |
| 11026607 |      110 |  11026347 | 11026347 |        0 |    |
| 11026636 |      110 |  11026607 | 11026347 |        0 |    |
| 11026763 |      110 |  11026347 | 11026347 |        0 |    |
| 11027916 |      110 |  11026347 | 11026347 |        0 |    |
| 11028437 |      110 |  11026347 | 11026347 |        0 |    |
| 11028465 |      110 |  11028437 | 11026347 |        0 |    |
| 11029511 |      110 |  11028465 | 11026347 |        0 |    |
| 11029601 |      110 |  11029511 | 11026347 |        0 |    |
| 11029625 |      110 |  11026347 | 11026347 |        0 |    |
| 11029777 |      110 |  11029601 | 11026347 |        0 |    |
| 11030151 |      110 |  11029777 | 11026347 |        0 |    |
| 11030179 |      110 |  11029625 | 11026347 |        0 |    |
| 11030832 |      110 |  11026347 | 11026347 |        0 |    |
+----------+----------+-----------+----------+----------+----+
这只是一个根节点都为11026347的数据集合~ 

解决方案 »

  1.   

    试试,分别在新的post_new和detail_new中加个新字段,叫post_newId,并且设定他们为auto_increment如果程序要用新的id就用新的id,老的id的话就依然使用原来的post_Id咯
      

  2.   

    to klan:
    auto_increment 能解决问题么?我想我的问题应该是具有同样root_id的数据集每个节点自己的post_id变了以后,各自的parent_id,root_id,last_id该如何跟着变化~ 而又如何用sql实现?
      

  3.   

    这个问题其实没有涉及到树的重构问题,从楼主的表述来看,其实只是各个节点号id的重命名而已。
    post表,和detail表的结构都没有发生任何的变化,他们之间其实也没有直接的关联查询。关键就是post_id和post_id_new直接的一个映射;因为parent_id和root_id(除0以外),一定是post_id的某个值,因此只要确定post_id就可以了.
    更改某个post_id时候,更改其对应在parent_id和root_id中的值
    ------
    我好久没有写程序了,mysql的语法也不熟悉.就说说我自己的流程,错误的地方见笑了。
    --
    1.复制一样的新表:post_new和detail_new
    2.创建一个映射表:Tmap
    Order(auto_increment) post_id(原来的)  post_id_new(新的id,最简单即是auto_increment)
      1
      2
      3
      4
      5
    ----
    3.select count(*) into XNumber from TmapFor cnt = 1 To XNumber Doselect post_id, post_id_new into Xpost_id, Xpost_id_new from Tmap where Order = cnt;update post_new set post_id = Xpost_id_new where post_id = Xpost_id;
    update post_new set parent_id = Xpost_id_new where parent_id = Xpost_id;
    update post_new set root_id = Xpost_id_new where root_id = Xpost_id;update detail_new set post_id = Xpost_id_new where post_id = Xpost_id;cnt = cnt + 1;ENDDO4. 删除Tmap表
       
      

  4.   

    对于 last_id, 也是一样,在loop中
    update post_new set last_id = Xpost_id_new where last_id = Xpost_id;