简体   繁体   中英

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)..

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

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 **

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

thanks for your attention

I'VE UPDATED MY CODED ACCORDING TO PSEUDOCODE FROM 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

for example row "3 A 2" in table1, supposed to be generating result in table2:

3 A 2

2 F 1

1 C 0

but my code only return like:

3 A 2

2 F 1

and the other rows generating the same result, only 2 rows

you will need a recursive function which is responsible for deep-copying the object like this:

  • copy my content to table 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.

hope this makes sense to you...

The recurvise function might look like:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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