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 8637527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:29:53+00:00 2026-06-12T10:29:53+00:00

Assuming I have something along the lines of this example: CREATE TABLE NaiveTable {

  • 0

Assuming I have something along the lines of this example:

CREATE TABLE NaiveTable
{
    id BIGINT NOT NULL, 
    parentId BIGINT NULL,
    name VARCHAR(20) NULL,
    CONSTRAINT hierarchy FOREIGN KEY (parentId) REFERENCES NaiveTable(id)
    PRIMARY KEY (id)
}

As a note parentId is a reference to id of NaiveTable (in case I missed the exact syntax).

The data are somewhere along the lines of these

+---------+----------+----------+
|   id    | parentId |  name    | 
+---------+----------+----------+
|   1     |   null   |  node1   |
+---------+----------+----------+
|   2     |     1    |  node2   |
+---------+----------+----------+
|   3     |     1    |  node3   |
+---------+----------+----------+
|   4     |     2    |  node4   |
+---------+----------+----------+

Column name contains some unrealated labels. I’m looking for a way to construct an SQL query on a MySQL table where all information will be flattened and sorted hierarchically like this:

node 1, depth 0
node 2, depth 1
node 4, depth 2
node 3, depth 1

NOTE: I can’t modify the database schema in any way. I can only create SQL queries. Also I can’t use WITH keyword since MySQL doesn’t support it. Is there a way to make such query?
However, any solution to depth two or beyond is considered good enough.

EDIT: Here is SQL fiddle if you love to experiment 🙂

  • 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-06-12T10:29:53+00:00Added an answer on June 12, 2026 at 10:29 am

    If the database’s schema is fixed and you can not add/edit any table, all you can do is to construct the tree in memory (in some programming language) and then try to calculate each node’s depth in memory. So my answer is that you can not produce your desired output with just one query!

    But if you could modify the schema of your database then you might want to check this out.

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

Sidebar

Related Questions

Assuming I have something like this: <div class=testdiv> <a href=/link1>link 1</a> </div> <div class=testdiv>
Suppose we have something along these lines int f(int n); .... do{ int a
Assuming I have something like the following: var address = {id: 100, name: Gregg,
Sorry for noob question. Assuming we have a model with relation: class Book(models.Model): name
Assuming I have a numerical string: var foo = 0; Assume I want to
Assuming I have a ASP.NET MVC 3 application that runs in a web farm
assuming I have the following array: views = [ { :user_id => 1, :viewed_at
Assuming i have a post model with created_at, title, and id. How can i
Assuming I have a column called A and I want to check if A
Assuming you have only the URL to a file (hosted on the same server

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.