Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6727469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:03:11+00:00 2026-05-26T10:03:11+00:00

Org_table: OrgName Id ParentID Org1 20 Null Org2 21 20 Org3 22 21 Org4

  • 0

Org_table:

OrgName    Id    ParentID
 Org1      20    Null
 Org2      21    20
 Org3      22    21
 Org4      23    21
 Org5      24    22
 Org6      25    Null

In the table above Org1 is the super organisation and Org2 is the child of Org1. And Sub Org of Org2 is Org3 and Org4. and Org 5 is sub Org of Org3

I want to display all the details of these Org under the parent Org1 in my data grid. Here I know only the Org1 ID
so mysql query is like

Select OrgName from Org_table where ID= 20 and ParentID is 20;

But this query returns only Org1 and Org2, I have to return Org3, Org4 and Org5 also because it is under Org1. How can I do it. should I use some kind of loop if so how to do it. I am using c# do this is in my asp.net website.

During runtime I wont know which org is child of which ORg. SO I have to find the If the parent id is under the main Org

I want to do recursive query

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:03:12+00:00Added an answer on May 26, 2026 at 10:03 am

    I don’t have mysql on hand, but this should do what you need

    CREATE TEMPORARY TABLE TempTable (OrgName VARCHAR(10), Id INT, ParentID INT); 
    
    INSERT INTO TempTable SELECT OrgName, Id, ParentID FROM org_table;
    
    WHILE EXISTS (SELECT TOP 1 1 FROM TempTable WHERE EXISTS(SELECT TOP 1 1 FROM TempTable TempTableParent WHERE TempTableParent.ID = TempTable.ParentID AND TempTableParent.ParentID IS NOT NULL) ) DO
    
    UPDATE TempTable SET ParentID = TempTableParent .parentID
    FROM TempTable
    INNER JOIN TempTable TempTableParent ON TempTableParent.id = TempTable.ParentID
    WHERE TempTable.ParentID IS NOT NULL AND TempTableParent.ParentID IS NOT NULL
    
    END WHILE;
    
    SELECT * FROM TempTable
    

    Not sure if the syntax on the update statement is entirely correct for mysql.. might need to be tweeked a little.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a (simplified) table: OrgName | Hierarchy ---------|------------ Org1 | A Org2 |
HI ! This is my table: CREATE TABLE [ORG].[MyTable]( .. [my_column2] UNIQUEIDENTIFIER NOT NULL
I currently have a Table [org.eclipse.swt.widgets.Table] with several TableColumns; however, due to UI space
I'm using the following org.eclipse.jface.viewers.CheckboxCellEditor.CheckboxCellEditor(Composite parent) I'm creating a table viewer with cellEditors and
I've two master tables Org & Item and then there's an OrgItem table.I have
http://en.wikipedia.org/wiki/ICalendar I'm working to implement an export feature for events. The link above lists
I'm trying to make a SELECT where I need to select a specified table
I created a report that pulled 1 object from the Purchase_order table (PO_REF) and
Suppose a Table per subclass inheritance relationship which can be described bellow (From wikibooks.org
I'm trying to create Class Table Inheritance as in ( http://www.castleproject.org/activerecord/documentation/trunk/usersguide/typehierarchy.html ) So let's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.