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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:02:40+00:00 2026-05-11T13:02:40+00:00

I have a MySQL database table with this structure: table id INT NOT NULL

  • 0

I have a MySQL database table with this structure:

table     id INT NOT NULL PRIMARY KEY     data ..     next_id INT NULL 

I need to fetch the data in order of the linked list. For example, given this data:

 id | next_id ----+---------   1 |       2   2 |       4   3 |       9   4 |       3   9 |    NULL 

I need to fetch the rows for id=1, 2, 4, 3, 9, in that order. How can I do this with a database query? (I can do it on the client end. I am curious if this can be done on the database side. Thus, saying it’s impossible is okay (given enough proof)).

It would be nice to have a termination point as well (e.g. stop after 10 fetches, or when some condition on the row turns true) but this is not a requirement (can be done on client side). I (hope I) do not need to check for circular references.

  • 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. 2026-05-11T13:02:41+00:00Added an answer on May 11, 2026 at 1:02 pm

    Some brands of database (e.g. Oracle, Microsoft SQL Server) support extra SQL syntax to run ‘recursive queries’ but MySQL does not support any such solution.

    The problem you are describing is the same as representing a tree structure in a SQL database. You just have a long, skinny tree.

    There are several solutions for storing and fetching this kind of data structure from an RDBMS. See some of the following questions:

    • ‘What is the most efficient/elegant way to parse a flat table into a tree?‘
    • ‘Is it possible to make a recursive SQL query ?‘

    Since you mention that you’d like to limit the ‘depth’ returned by the query, you can achieve this while querying the list this way:

    SELECT * FROM mytable t1  LEFT JOIN mytable t2 ON (t1.next_id = t2.id)  LEFT JOIN mytable t3 ON (t2.next_id = t3.id)  LEFT JOIN mytable t4 ON (t3.next_id = t4.id)  LEFT JOIN mytable t5 ON (t4.next_id = t5.id)  LEFT JOIN mytable t6 ON (t5.next_id = t6.id)  LEFT JOIN mytable t7 ON (t6.next_id = t7.id)  LEFT JOIN mytable t8 ON (t7.next_id = t8.id)  LEFT JOIN mytable t9 ON (t8.next_id = t9.id)  LEFT JOIN mytable t10 ON (t9.next_id = t10.id); 

    It’ll perform like molasses, and the result will come back all on one row (per linked list), but you’ll get the result.

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

Sidebar

Ask A Question

Stats

  • Questions 88k
  • Answers 88k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That's fine... I don't know if the GL|ES on the… May 11, 2026 at 5:47 pm
  • Editorial Team
    Editorial Team added an answer SELECT CONVERT(char(10), GetDate(),126) Limiting the size of the varchar chops… May 11, 2026 at 5:47 pm
  • Editorial Team
    Editorial Team added an answer In Python, a decorator is a function that takes a… May 11, 2026 at 5:47 pm

Related Questions

I have a MySQL table with coordinates; the column names are X and Y.
I have a few classes, such as those that outline database table structure or
I'm trying to count multiple entries in a MySQL database, I know how to
[edit] We're collecting credit application data from users on a web form. I have

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.