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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:38:35+00:00 2026-05-27T07:38:35+00:00

I have a categories table in MySql something like this: categoryId | categoryTitle |

  • 0

I have a categories table in MySql something like this:

categoryId | categoryTitle | definedField | parentId
    1              Title         123           NULL
    2          AnotherTitle       234          1
    3         AndAnotherOne       NULL         1

What I need to do is find the closest definedField value by going up to parent,like this;

Since category 2 has a definedField, return its value;
Since category 3 does not have a definedField, search up, to its parent. It has definedField, so return it. If it didn’t have one, search up until find one.

There will ALLWAYS be the topmost category that will have definedField set. I only need to find a good algorithm to search for this in a MySQL InnoDb table.

  • 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-27T07:38:36+00:00Added an answer on May 27, 2026 at 7:38 am

    There is no direct way of retrieving hierarchical data in MySQL (like, for example, Postgres’s RECURSIVE query). There is a good article summarizing different ways of implementing nested data set in MySQL: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

    Most users at one time or another have dealt with hierarchical data in
    a SQL database and no doubt learned that the management of
    hierarchical data is not what a relational database is intended for.
    The tables of a relational database are not hierarchical (like XML),
    but are simply a flat list. Hierarchical data has a parent-child
    relationship that is not naturally represented in a relational
    database table.

    The article covers two models: Adjacency List and Nested Set.

    The Adjacency List Model

    In the adjacency list model, each item in the table contains a pointer
    to its parent. The topmost element, in this case electronics, has a
    NULL value for its parent. The adjacency list model has the advantage
    of being quite simple, it is easy to see thatFLASH is a child ofmp3
    players, which is a child of portable electronics, which is a child of
    electronics. While the adjacency list model can be dealt with fairly
    easily in client-side code, working with the model can be more
    problematic in pure SQL.

    The Nested Set Model

    In the Nested Set Model, we can look at our hierarchy in a new way,
    not as nodes and lines, but as nested containers.

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

Sidebar

Related Questions

I have a MySQL table like this: CREATE TABLE categories ( ID INT NOT
I have a simple MySQL table thats contains a list of categories, level is
I have a Categories table in which each category has a ParentId that can
I have a table named categories, which contains ID(long), Name(varchar(50)), parentID(long), and shownByDefault(boolean) columns.
I have a table in SQL server: Categories -------------- CategoryID (uniqueidentifier) ParentCategoryID (uniqueidentifier) allow
If I have a table which looked something like (`id`, `category`, `content`, `post_time`) And
is this good way to code breadcrumbs? create table categories using mysql create after
I have a MYSQL table called 'categories' from a project I inherited from someone
I have a mysql table containing news data and categories and i'm trying to
I have something like Facebook's Wall build on PHP that uses MySQL database. Structure:

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.