Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Self referential join

Self referential join

Scheduled Pinned Locked Moved Database
databasemysqltutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Johnny
    wrote on last edited by
    #1

    I have a table with a parent-child relationship in a MySQL database: id parent_id - points to id other columns I can easily extract all rows belonging to a particular parent. For example: SELECT * FROM table WHERE parent_id=5 What I'm trying to do is extend this to not only get all rows belonging to a particular parent, but also get a count of each row's children (the grandchildren). I don't need any further generations. Other than retrieving the children and iterating through the rows and fetching the counts seperatley, I've been getting into a good muddle with joins and self-referential joins. I'm hoping that some kind soul has a few suggestions!

    D 1 Reply Last reply
    0
    • J Johnny

      I have a table with a parent-child relationship in a MySQL database: id parent_id - points to id other columns I can easily extract all rows belonging to a particular parent. For example: SELECT * FROM table WHERE parent_id=5 What I'm trying to do is extend this to not only get all rows belonging to a particular parent, but also get a count of each row's children (the grandchildren). I don't need any further generations. Other than retrieving the children and iterating through the rows and fetching the counts seperatley, I've been getting into a good muddle with joins and self-referential joins. I'm hoping that some kind soul has a few suggestions!

      D Offline
      D Offline
      DQNOK
      wrote on last edited by
      #2

      This seems to work on my system (not MySQL). SELECT * , (SELECT COUNT(*) FROM tblTasks WHERE parentTaskID=T.taskID) FROM tblTasks AS T WHERE T.parentTaskID=5; Don't know whether MySQL will support the (SELECT ...). Hope this helps.

      J 1 Reply Last reply
      0
      • D DQNOK

        This seems to work on my system (not MySQL). SELECT * , (SELECT COUNT(*) FROM tblTasks WHERE parentTaskID=T.taskID) FROM tblTasks AS T WHERE T.parentTaskID=5; Don't know whether MySQL will support the (SELECT ...). Hope this helps.

        J Offline
        J Offline
        Johnny
        wrote on last edited by
        #3

        That is perfect, thank you! And a whole lot simpler than my left joins

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups