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

The Archive Base Latest Questions

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

Assume the following table records: TABLE: foo ========================== | foo_id | foo_parent_id | ==========================

  • 0

Assume the following table records:

TABLE: foo
==========================
| foo_id | foo_parent_id |
==========================
| 1      | NULL          |
| 2      | NULL          |
| 3      | 1             |
| 4      | 2             |
| 5      | 1             |
| 6      | 1             |
| 7      | 2             |
| 8      | 1             |
| 9      | NULL          |
--------------------------

I want to get, say, the first 10 parent records (those records with foo_parent_id = NULL) immediately followed by, say, the first 2 child records of that parent record. So, I’m looking for a result like this:

1, NULL
3, 1
5, 1
2, NULL
4, 2
7, 2
9, NULL

How do I query something like this?

  • 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-27T17:35:17+00:00Added an answer on May 27, 2026 at 5:35 pm

    Here’s one idea. But it’s based on lots of assumptions about the way your data is setup. Ever increasing IDs down the tree, only two levels, etc.

    SELECT f.foo_id,f.foo_parent_id FROM foo f
    foo f
    

    –give me the top X number of parent_ids
    (This is good, you just adjust the LIMIT 10 to vary the number of parent levels to show)

    INNER JOIN 
    (select foo_id from foo where foo_parent_id is null order by foo_parent_id 
    LIMIT 10
    ) top_foo_parent
          on isnull(f.foo_parent_id,f.foo_id) = top_foo_parent.foo_id
    WHERE
    

    (This part is kind of hacky, as you have to put an ever longer string of these to get past two children)

    –it’s the first child, or…

    (f.foo_id in (select MIN(foo_id) from foo fc1 where fc1.foo_parent_id =f.foo_parent_id)
     )
     or
    

    –it’s the second child, or…

    (f.foo_id in (select MIN(foo_id) from foo fc1 where fc1.foo_parent_id =f.foo_parent_id  and fc1.foo_id not in (select MIN(foo_id) from foo fc2 where fc2.foo_parent_id=f.foo_parent_id))
     )
     or 
    

    –it’s the parent

     f.foo_parent_id is null
    order by isnull(f.foo_parent_id,f.foo_id)*100 + f.foo_id
    

    So what we’re doing here is basically ordering by the parent_id column and then the child columns underneath it with a slight twist. If the parentid column is NULL then we use the actual ID. This means that for ordering purposes our table looks like this:

    ==============================================================================
    | foo_id | foo_parent_id |   isnull(f.foo_parent_id,f.foo_id)
    ==============================================================================
    | 1      | NULL           |         (1)
    | 2      | NULL           |         (2)
    | 3      |  1             |         1
    | 4      |  2             |         2
    | 5      |  1             |         1
    | 7      |  2             |         2
    ----------------------------------------------------------------------
    

    Then we multiply that ordering column *100

    ==============================================================================
    | foo_id | foo_parent_id |   isnull(f.foo_parent_id,f.foo_id)*100
    ==============================================================================
    | 1      | NULL           |         100
    | 2      | NULL           |         200
    | 3      |  1             |         100
    | 4      |  2             |         200
    | 5      |  1             |         100
    | 7      |  2             |         200
    ----------------------------------------------------------------------
    

    and lastly we add our foo_id column to it

    ==============================================================================
    | foo_id | foo_parent_id |   isnull(f.foo_parent_id,f.foo_id)*100 + foo_id
    ==============================================================================
    | 1      | NULL           |         101
    | 2      | NULL           |         202
    | 3      |  1             |         103
    | 4      |  2             |         204
    | 5      |  1             |         105
    | 7      |  2             |         207
    ----------------------------------------------------------------------
    

    Now we order the table by that virtual column and…

    ==============================================================================
    | foo_id | foo_parent_id |   ORDER BY isnull(f.foo_parent_id,f.foo_id)*100 + foo_id
    ==============================================================================
    | 1      | NULL           |         101
    | 3      |  1             |         103
    | 5      |  1             |         105
    | 2      | NULL           |         202    
    | 4      |  2             |         204
    | 7      |  2             |         207
    ----------------------------------------------------------------------
    

    There we go!

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

Sidebar

Related Questions

Assume I have the following style table, col1 col2 and col3 have same value
Assume a table with the following columns: pri_id , item_id , comment , date
Assume the following type definitions: public interface IFoo<T> : IBar<T> {} public class Foo<T>
assume this following function: int binaryTree::findHeight(node *n) { if (n == NULL) { return
Assume the following mapping entries: < class name=Customer table=customer> <id name=InternalId column=uintCustomerId type=long> <generator
Assume we have the following table: Id A B 1 10 ABC 2 10
Assume the following in MySQL: CREATE TABLE users ( id integer auto_increment primary key,
My simplified and contrived example is the following:- Lets say that I want to
Given the following table partitioning under PostgreSQL 9.0.3: CREATE TABLE records ( ts TIMESTAMP,
Let's assume that I have two tables... Foo and Bar. They contain the following

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.