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

  • Home
  • SEARCH
  • 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 8406179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:02:01+00:00 2026-06-09T23:02:01+00:00

I have a very basic one table scenario; tblFamiles entryID : Int name: Text

  • 0

I have a very basic one table scenario;

tblFamiles
entryID : Int
name: Text
parentID: Int

Lets add the following rows;

entryID : name                  : parentID
1       : Grandmother Jones     : 0
2       : Grandmother Thompson  : 0
3       : Mother Jones          : 1
4       : Mother Thompson       : 2
5       : 1st Daughter Jones    : 3
6       : 2nd Daughter Jones    : 3
7       : 1st Daughter Thompson : 4

Here we have three generations of two familes stored, the Jones family and the Thompson family (as an exampe). I would like to query this table but order the results by the parentID (but not just like plain old ORDER BY 'parentID' DESC, so that they are in relative order. I would like an output like this;

SELECT (SOME MAGIC) FROM `tblFamiles`;
entryID : name                  : parentID
1       : Grandmother Jones     : 0
3       : Mother Jones          : 1
5       : 1st Daughter Jones    : 3
6       : 2nd Daughter Jones    : 3
2       : Grandmother Thompson  : 0
4       : Mother Thompson       : 2
7       : 1st Daughter Thompson : 4

Logically, the only way I can see how to do this is to loop through all entryIDs and then for each entryID; loop through all other records checking their parentID fields against the current entryID, and bringing those records to the top of the result set, under the current row. But I can’t see how to do that in MySQL.

UPDATE

I have used families as an example above, but what I am after is a method of storing nested entries and getting them in a single query, for efficiency really. I could just make multiple SELECT queries but that would be it ugly;

(Pseudo)
SELECT entryID, name WHERE parentID = 0 LIMIT 0,1;
print name;
 Sub query:
 SELECT entryID, name WHERE parentID = $above-entryID
 print name;
  (Keep looping through this till the second query returns no results, 
  then go back to the first query and move onto the next entryID)

2nd UPDATE

You can forget the name column even exists, I just used that as an example, all that matters here is entryID and parentID as these are the two columns that link and control everything. There could be twenty extra columns as well as name, but they all revolve around entryID and parentID, it’s just linked or nested (which term ever is more appropriate) IDs.

  • 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-09T23:02:03+00:00Added an answer on June 9, 2026 at 11:02 pm

    “But I can’t see how to do that in MySQL.”

    Short answer

    You can’t.

    Slight longer answer.

    The problem you’re trying to solve is not one that a relational database is a good solution for solving. What you’re trying to do would require a Object-relational database.

    For the theory I’d suggest reading the difference between the two:

    http://en.wikipedia.org/wiki/Relational_database

    http://en.wikipedia.org/wiki/Object-relational_database

    As well as questions on stackoverflow about why each are good/bad.

    https://stackoverflow.com/questions/800/object-oriented-vs-relational-databases or
    Object-oriented-like structures in relational databases

    Including this answer https://stackoverflow.com/a/600765/778719 which leads to
    The Vietnam of Computer Science.

    That is actually more about object mapping into a relational database, but it does show you the depth (and lack of obvious solutions) to the problem you’ve just encountered.

    The actual problem with using one when you actually need the other is called the Object-relational impedance mismatch
    http://en.wikipedia.org/wiki/Object-relational_impedance_mismatch

    Actually possibly useful answer

    The problem you described is one that is best handled by objects. I suggest just stick with storing them in a relational database for now, just accept the fact that your logic will need to live in the application code layer, not the SQL layer, and so make multiple queries to the database if you need to to fetch each ‘object’ or possibly each ‘layer’ of objects.

    That will work up until a point, at which point you will understand the problem better and be in a better position to either use a different technology, or at least understand the tradeoffs between different solutions.

    Horrifying bonus

    Cycles in family tree software

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

Sidebar

Related Questions

I have only one table in my mySql database for a very basic website
I have a very basic mysql table called memberships, that tracks which people belong
I have a very basic SSIS package with one data flow task (from an
I have a very basic table: <table id=ttable5 class=table-default> <thead> <tr> <th>Nombre</th> <th class=sort-date>Provincia</th>
I have the following table of counters: CREATE TABLE cache ( key text PRIMARY
I have a very basic iphone app where the following steps occur. App Delegate
I have a very basic API in my app which also has an index
I have a very basic windows forms app, working in C#, and I was
I have a very basic app with two ignition-remoteimageviews (RIV) in them. The app
I have some very basic semaphore code that works great on Linux, but cannot

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.