Recursive Query with MySQL ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • qw12er
    Confirmed User
    • Apr 2004
    • 799

    #1

    Recursive Query with MySQL ?

    Basicaly I need to fetch the root parent of any object child. (could be as low as 25 level deep into the tree structure.)

    Is there a way to do it in mysql ? I've seen it in SQLServer with the "With" statement.

    heres my db columns:

    dbID idParent idChild
    1 2
    1 3
    2 4
    2 5
    4 6


    For example, I need to query to return 1 if I look for idChild = 6

    thanks
    I have nothing to advertise ... yet.
  • redwhiteandblue
    Bollocks
    • Jun 2007
    • 2793

    #2
    Think you would need a stored procedure for that, but not sure as I've never done one in MySQL.
    Interserver unmanaged AMD Ryzen servers from $73.00

    Comment

    • modF
      Confirmed User
      • Aug 2002
      • 1888

      #3
      Last I checked, the WITH clause was not supported in MySQL.

      I do things
      skype:themodF

      Comment

      • qw12er
        Confirmed User
        • Apr 2004
        • 799

        #4
        Originally posted by modF
        Last I checked, the WITH clause was not supported in MySQL.
        It's not ... and this is why I'm having trouble.
        I have nothing to advertise ... yet.

        Comment

        • KillerK
          Confirmed User
          • May 2008
          • 3406

          #5
          http://stackoverflow.com/questions/3...ql-with-clause

          Comment

          • donborno
            Confirmed User
            • Jan 2007
            • 374

            #6
            Google Nested set model

            Comment

            • Tempest
              Too lazy to set a custom title
              • May 2004
              • 10217

              #7
              Originally posted by qw12er
              Basicaly I need to fetch the root parent of any object child. (could be as low as 25 level deep into the tree structure.)

              Is there a way to do it in mysql ? I've seen it in SQLServer with the "With" statement.

              heres my db columns:

              dbID idParent idChild
              1 2
              1 3
              2 4
              2 5
              4 6


              For example, I need to query to return 1 if I look for idChild = 6

              thanks
              Can't tell what you're trying to accomplish with that table. Seems to be missing data.

              Regardless, it sounds like you real problem is that the DB design sucks and you'd just have to do multiple queries..

              Comment

              • qw12er
                Confirmed User
                • Apr 2004
                • 799

                #8
                Originally posted by donborno
                Google Nested set model

                Problem ain't speed ... it's getting root parent...
                I have nothing to advertise ... yet.

                Comment

                • vdbucks
                  Monger Cash
                  • Jul 2010
                  • 2773

                  #9
                  Originally posted by qw12er
                  Basicaly I need to fetch the root parent of any object child. (could be as low as 25 level deep into the tree structure.)

                  Is there a way to do it in mysql ? I've seen it in SQLServer with the "With" statement.

                  heres my db columns:

                  dbID idParent idChild
                  1 2
                  1 3
                  2 4
                  2 5
                  4 6


                  For example, I need to query to return 1 if I look for idChild = 6

                  thanks
                  Not really sure what you're trying to accomplish as it doesn't appear as though your example matches your explanation.

                  Are you trying to do something like:

                  Code:
                  idParent* = 1
                  |
                  ---> idChild => 2,3,4,5,6 #so on and so forth
                  From the sounds of your description, this is what you mean.. but from your example it's not. But then, the example you gave doesn't show how the idChild 6 has the root idParent of 1 to begin with... Unless of course I'm missing something.

                  All in all, more information is needed.

                  *I'm not including additional idParent values as of now in order to get an exact idea of what you're trying to accomplish.
                  Last edited by vdbucks; 03-16-2011, 07:58 PM.

                  Comment

                  • sheken
                    Confirmed User
                    • Sep 2002
                    • 135

                    #10
                    Originally posted by donborno
                    Google Nested set model
                    ^^

                    this

                    Comment

                    • Tempest
                      Too lazy to set a custom title
                      • May 2004
                      • 10217

                      #11
                      Originally posted by vdbucks
                      Not really sure what you're trying to accomplish as it doesn't appear as though your example matches your explanation.

                      Are you trying to do something like:

                      Code:
                      idParent* = 1
                      |
                      ---> idChild => 2,3,4,5,6 #so on and so forth
                      From the sounds of your description, this is what you mean.. but from your example it's not. But then, the example you gave doesn't show how the idChild 6 has the root idParent of 1 to begin with... Unless of course I'm missing something.

                      All in all, more information is needed.

                      *I'm not including additional idParent values as of now in order to get an exact idea of what you're trying to accomplish.
                      I "think" what he's looking for is to keep checking for a parent until there are none... So if he starts with idChild = 6, it's parent is 4.. Then he checks child 4 exists and finds it has a parent of 2.. Then he checks child 2 and finds it exists with a parent of 1.. Then he checks child 1 and find there is no child so the root parent is 1.

                      Comment

                      • qw12er
                        Confirmed User
                        • Apr 2004
                        • 799

                        #12
                        Originally posted by Tempest
                        I "think" what he's looking for is to keep checking for a parent until there are none... So if he starts with idChild = 6, it's parent is 4.. Then he checks child 4 exists and finds it has a parent of 2.. Then he checks child 2 and finds it exists with a parent of 1.. Then he checks child 1 and find there is no child so the root parent is 1.
                        Exactly !!

                        Sorry if my explanations weren't clear.
                        (The html kind of scrw up my table)
                        I have nothing to advertise ... yet.

                        Comment

                        • vdbucks
                          Monger Cash
                          • Jul 2010
                          • 2773

                          #13
                          Originally posted by Tempest
                          I "think" what he's looking for is to keep checking for a parent until there are none... So if he starts with idChild = 6, it's parent is 4.. Then he checks child 4 exists and finds it has a parent of 2.. Then he checks child 2 and finds it exists with a parent of 1.. Then he checks child 1 and find there is no child so the root parent is 1.
                          I figured this was what he was trying to do but didn't want to assume.. it appears as though you're right though from his reply to this..

                          @qw12er - Now let me ask.. is this in php?

                          Comment

                          • qw12er
                            Confirmed User
                            • Apr 2004
                            • 799

                            #14
                            Originally posted by vdbucks
                            I figured this was what he was trying to do but didn't want to assume.. it appears as though you're right though from his reply to this..

                            @qw12er - Now let me ask.. is this in php?
                            php 5.3 -yup
                            I have nothing to advertise ... yet.

                            Comment

                            • vdbucks
                              Monger Cash
                              • Jul 2010
                              • 2773

                              #15
                              Originally posted by qw12er
                              php 5.3 -yup
                              Have you tried pulling up the entire query and using maybe a foreach or while loop?

                              Can it be a simple matter of say...

                              <?php
                              $idChild = 6 //however you get idChild value

                              // create a query first in the event you have to do things with the query like mysql_real_escape_string and whatnot
                              $query = sprintf("SELECT idParent, idChild FROM dbtable WHERE idChild = '%s'");

                              //process the query
                              $result = mysql_query($query);

                              while ($row = mysql_fetch_assoc($result)) {
                              // do stuff
                              echo $row['idChild'];
                              echo $row['idParent'];
                              }
                              ?>
                              Last edited by vdbucks; 03-16-2011, 09:24 PM.

                              Comment

                              • vdbucks
                                Monger Cash
                                • Jul 2010
                                • 2773

                                #16
                                Additionally, if you just want to get all idChild values and match them with idParent values, you can modify the above $query to:

                                $query = sprintf('SELECT idParent, idChild FROM dbtable');

                                Then change the while line to:

                                while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                                //do stuff
                                echo $row['idChild'];
                                echo $row['idParent'];
                                }

                                Comment

                                • qw12er
                                  Confirmed User
                                  • Apr 2004
                                  • 799

                                  #17
                                  Originally posted by vdbucks
                                  Have you tried pulling up the entire query and using maybe a foreach or while loop?

                                  Can it be a simple matter of say...

                                  Yeah that's is sort of what I did but this solutions isn't clean as it could be.
                                  (makes way to much query to DB for nothing)
                                  I have nothing to advertise ... yet.

                                  Comment

                                  • HarryMuff
                                    Confirmed User
                                    • Dec 2005
                                    • 271

                                    #18
                                    Derp de derp, ta teetley tum

                                    Comment

                                    • donborno
                                      Confirmed User
                                      • Jan 2007
                                      • 374

                                      #19
                                      Originally posted by qw12er
                                      Problem ain't speed ... it's getting root parent...
                                      Which is very simple using the nested set model

                                      Comment

                                      Working...