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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:23:21+00:00 2026-05-22T23:23:21+00:00

I want to fetch some data from the following database table ‘article’. i want

  • 0

I want to fetch some data from the following database table ‘article’.
enter image description here

i want to fetch an article information whose category_id is 4. But the result should contain all the records of its parent categories.

for example :
‘article1’ has parent ‘Sub Category1’ and its parent is ‘Main Category’. So the result should have the rows 1,2,4 from the table.

Is it possible to write one SQL query for this? what will be the query which can fetch data according to above condition?

Please guide me..!!

thanks..

  • 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-22T23:23:22+00:00Added an answer on May 22, 2026 at 11:23 pm

    Please refer to this article: Managing Hierarchical Data in MySQL.

    Actually, they have purposed model to get Hierarchical data without recursion.
    In that they use lft(left) and rgt(right) this to extra column for storing structural information of table. lft and rgt are set as follows

    Root lft is 1. then its first child lft is next number then its child in next number so on till there are no child then for that node (leaf node) rgt will be its lft +1.
    And we will set siblings lft as rgt +1 and follow same rules for it also.

    And if all child’s numbering is done its will set parents rgt to last child’s rgt +1.

    I haven’t explain it neatly but on link with image it is easier to understand.

    So after this you can easily explore nested structure using following queries

    For getting all parent’s id in order:

    SELECT parent.category_id
    FROM article AS node,
    article AS parent
    WHERE node.lft BETWEEN parent.lft AND parent.rgt
    AND node.category_id = $category_id
    ORDER BY parent.lft;
    

    For DELETING any row :

    LOCK TABLE article WRITE;
    SELECT @myLeft := lft, @myRight := rgt, @myWidth := rgt - lft + 1
      FROM article WHERE category_id = 'row_id';
    DELETE FROM article WHERE lft BETWEEN @myLeft AND @myRight;
    UPDATE article SET rgt = rgt - @myWidth WHERE rgt > @myRight;
    UPDATE article SET lft = lft - @myWidth WHERE lft > @myRight;
    UNLOCK TABLES;
    

    For inserting any row:

    LOCK TABLE article WRITE;
    SELECT @myLeft := lft FROM article  WHERE category_id = 'parent_id';
    UPDATE article SET rgt = rgt + 2 WHERE rgt > @myLeft;
    UPDATE article SET lft = lft + 2 WHERE lft > @myLeft;
    INSERT INTO article(title, lft, rgt) VALUES('title', @myLeft + 1, @myLeft + 2);
    UNLOCK TABLES;
    

    This is somewhat complex, but after creating stored procedure, it will not be difficult to use.

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

Sidebar

Related Questions

I have console application. In that i have some process that fetch the data
I want to create application to fetch the current location data using Java script.
I have a large data in to table and i want to populate it
I want to reference a COM DLL in a .NET project, but I also
I want to log onto Stack Overflow using OpenID, but I thought I'd set
I have entries for an enumeration stored inside a database table with only the
I would like some help with a OOD query. Say I have the following
I get the data from DB and display it in a div... what I
I have some basic questions about core data (which I am new to) and
I have a table which maintains categories from another site. I populate this table

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.