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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:47:19+00:00 2026-06-08T09:47:19+00:00

This is the more advanced sequel to an earlier question. Here’s a link to

  • 0

This is the more advanced sequel to an earlier question.

Here’s a link to an SQLFiddle to make things a bit clearer

I need to return something like (for each location):

Ground Floor
---------------
Garage
– Radiator
Kitchen
– Cooker
– Fridge

There’s going to be even more levels than this but if I can get started I should hopefully be able to go further as needed.

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-06-08T09:47:21+00:00Added an answer on June 8, 2026 at 9:47 am

    Since SQL cannot return nested records as you have formatted above, you need only to return a query which orders your rows by floor then by sub_location. You’ll get both the location and sub_location repeated for each row, but in your application code loop when printing the results, you format them as you have above.

    SELECT
      location.location_name,
      sublocation.sub_location_name,
      asset.asset_name
    FROM
      location
      LEFT JOIN sub_location ON location.location_key = sublocation.location_key
      LEFT JOIN assets ON sub_location.sub_location_key = assets.sub_location_key
    ORDER BY
      location.location_name,
      sub_location.sub_location_name
    

    When looping over your rowset in your application, you then only print the new location or sub_location when it changes. Formatting is done in code.

    Assuming your rows are all in the array $rowset:

    // Store location, sub_location for each loop
    $current_loc = "";
    $current_subloca = "";
    foreach ($rowset as $row) {
      // If the location changed, print it
      if ($row['location'] != $current_loc) {
        echo $row['location_name'] . "\n";
        // Store the new one
        $current_loc = $row['location_name'];
      }
      // If the sub_location changed, print it
      if ($row['sub_location'] != $current_subloc) {
        echo $row['sub_location_name'] . "\n";
        $current_subloc = $row['sub_location_name'];
      }
      echo $row['asset_name'] . "\n";      
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I'm going to try to make this more clear because my last question
This might be a more advanced CSS question about z-index : You can see
I need advice from more advanced SQL experts on this. I am being asked
A bit of a silly question for more advanced programmers, but In my quest
To make this more clear, I'm going to put code samples: $file = fopen('filename.ext',
This is more of a design question. I have a template class, and I
This is more of a hypothetical question. I am wondering if anyone knows if/how
I'm having trouble wrapping my head around more advanced Rails query methods. In this
Same idea as my previous question , just a little more advanced. I want
This is a follow-up to my previous question (found here ). My properties files

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.