简体   繁体   English

mysql在表与父子关系之间复制数据

[英]mysql copy data between table with parent child relation

i have two identical table 我有两个相同的表

the first table's structure is like a tree where each record acts like a node. 第一个表的结构就像一棵树,其中每个记录的行为就像一个节点。

each node has three field consist of: index as the node-id, item as the represent of item name, parents as pointer to the previous node(index).. 每个节点具有三个字段,其中包括: index作为节点ID, item作为项目名称的表示, parent作为指向前一个节点(索引)的指针。

as you can see at the picture each node related to each other based on their parent-index field relation 如您在图片中看到的,每个节点都基于其父索引字段关系相互关联

now what i wanna do is traversing each node(record) to the root, then arrange them into a new table with the same structure , so later on the 2nd table will have 21 records as seen on picture 现在我想做的是遍历每个节点(记录)到根,然后将它们安排到具有相同结构的新表中 ,因此稍后在第二个表上将有21条记录,如图所示

i'm using vb.net and for next looping to loop the index of each row, then insert each row into a new table.. but the problem is how can i ** copy the data from 1st table to 2nd table with condition parent-child ** 我正在使用vb.net,并在下一个循环中循环每一行的索引,然后将每一行插入到新表中。.但是问题是我如何在条件为条件的情况下将**从第一张表复制到第二张表-儿童**

here's the picture 这是图片

在此处输入图片说明

here's my pseudocode 这是我的伪代码

for i=1 to 11

checkindex = select * from 1st table where index = i

if checkindex.hasrows() then
traverse the row to its root then insert into 2nd table
end if

next i

so my question is, based on my pseudocode, how to code traversing each row then insert into 2nd table? 所以我的问题是,根据我的伪代码,如何编码遍历每一行然后插入第二张表?

i'm using mysql database and vb.net 我正在使用mysql数据库和vb.net

thanks for your attention 感谢您的关注

I'VE UPDATED MY CODED ACCORDING TO PSEUDOCODE FROM KJELL 我已经根据KJELL的伪代码更新了我的编码

For node = 1 To maxnode
        'QUERY A= FETCHING ROW/NODE FROM TABLE1 BASED ON INDEX
        Dim node1 As String = "select indeks, item, parent from tree where indeks = '" & node & "' "
        CMD_node = New MySqlCommand(node1, conn.konek)
        hasilnode = CMD_node.ExecuteReader()

        If hasilnode.HasRows Then
            'QUERY B = FETCH PARENT VALUE FROM CURRENT ROW
            Dim getpar1 As String = "select parent from tree where indeks = '" & node & "' "
            CMD_getpar = New MySqlCommand(getpar1, conn.konek)
            getpar = Convert.ToInt32(CMD_getpar.ExecuteScalar())

            'QUERY C = INSERT CURRENT ROW(TABLE1) TO TABLE2
            Dim rec_ins As String = "insert into node (indeks, item, parent, node_id) select indeks, item, parent, '" & node & "' from tree where indeks = '" & node & "' "
            CMD_recs = New MySqlCommand(rec_ins, conn.konek)
            CMD_recs.ExecuteNonQuery()
            CMD_recs.Connection.Dispose()

            'QUERY D = CHECKING IF CURRENT ROW'S PARENT = 0 (NOT EXIST) WITH PARENT VALUE ACHIEVED FROM QUERY B

            If getpar <> 0 Then
                'INSERT NEWROW WHERE NEWROW.INDEX = CURRENT ROW.PARENT
                Dim rec_ins2 As String = "insert into node(indeks, item, parent, node_id) select indeks, item, parent, '" & node & "' from tree where indeks = '" & getpar & "' "
                CMD_recs = New MySqlCommand(rec_ins2, conn.konek)
                CMD_recs.ExecuteNonQuery()
                CMD_recs.Connection.Dispose()
            End If
            'Else
            'Continue While
            'End If

            'End While

        Else
            Continue For
            CMD_getpar.Connection.Dispose()
        End If
        CMD_node.Connection.Dispose()

    Next node

    CMD_list.Connection.Dispose()

this code produce result just like my illustration but each current row in table1 only call the previous rows once , resulting like each row in table 1 (except row with parent 1) only generating two row in table2, which is the desired result is supposed to be: each row in table 1 generating different amount of rows in table2, according to the parent-index relation 此代码产生的结果与我的插图相同, 但是 table1中的每个当前行调用前一行一次 ,结果类似于表1中的每一行(父1的行除外)仅在table2中生成两行, 这是期望的结果是:表1中的每一行根据父索引关系在表2中生成不同数量的行

for example row "3 A 2" in table1, supposed to be generating result in table2: 例如,表1中的“ 3 A 2”行应该在表2中生成结果:

3 A 2 3 A 2

2 F 1 2楼1

1 C 0 1 C 0

but my code only return like: 但是我的代码只会返回:

3 A 2 3 A 2

2 F 1 2楼1

and the other rows generating the same result, only 2 rows 其他行产生相同的结果, 只有 2行

you will need a recursive function which is responsible for deep-copying the object like this: 您将需要一个递归函数,该函数负责像下面这样深度复制对象:

  • copy my content to table 2. 将我的内容复制到表2中。
  • check if i have a parent 检查我是否有父母
  • get the parent's content and start copy function with that content. 获取父母的内容,并使用该内容开始复制功能。

You should query table 1 for all your objects and in a loop call the recursive deep copy function. 您应该在表1中查询所有对象,并在循环中调用递归深度复制功能。

hope this makes sense to you... 希望这对你有意义...

The recurvise function might look like: recurvise函数可能类似于:

function storeMe(content) {
  storeToDb(content)
  myParentID = content[parent]
  if (myParentID != 0) {
    parentContent = getParentContent(myParentID)
    unset(parentContent[lineID])
    storeMe(parentContent)
  }
}

The main functionality could look like: 主要功能如下所示:

myObjects = getAllFromTable1()
for(myObjects) {
  storeMe(object)
}

One crucial thing here: if your table1 uses a unique id for each line in the DB you have to unset it when storing the parents, so a new element is written to the DB and not the same updated over and over again. 这里的关键是:如果您的table1为数据库中的每一行使用唯一的ID,则在存储父级时必须将其取消设置,因此会向数据库写入新元素,并且不会一次又一次地更新相同的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM