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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:27:08+00:00 2026-05-11T16:27:08+00:00

Some time ago I asked a question about nested loops on SO and as

  • 0

Some time ago I asked a question about nested loops on SO and as it was, there were queries inside the loops of my example and I got a clear answer:

NEVER EVER NEVER put an SQL query inside a loop

I’ve tried ever since and mostly it works. Just need to make an effort and write a query that retrieves all you need at once.

BUT what do you do when you have a dataset from a JOIN query which contains nested data which you need to output in a nested way?

Example join from table A and B:

A.a     |  B.a     |  B.b
--------|----------|-------------
fruits  |  banana  |  yellow
fruits  |  apple   |  red
animals |  zebra   |  black&white
animals |  elefant |  gray
animals |  fox     |  red
planets |  earth   |  blue
planets |  mars    |  red

ok, now I got that all in an array or rowset and now I need to display something like that:

fruits

  • yellow banana
  • red apple

animals

  • black&white zebra
  • gray elefant
  • red fox

planets

  • blue earth
  • red mars

it seems obvious that it should work but I’ve tried to wrap my mind around it several times now and I just can’t come up with a solution.

At the moment I do it my old way:

query groups

foreach groups
{
    query animals in group
    foreach animal
}

but hey, NEVER EVER NEVER put sql inside a loop. so what shold I do? I do PHP but I think this is a meta question.

  • 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-11T16:27:08+00:00Added an answer on May 11, 2026 at 4:27 pm

    Use the control break algorithm.

    I’d return a result set exactly as you show in the question:

    A.a     |  B.a     |  B.b
    --------|----------|-------------
    fruits  |  banana  |  yellow
    fruits  |  apple   |  red
    animals |  zebra   |  black&white
    animals |  elefant |  gray
    animals |  fox     |  red
    planets |  earth   |  blue
    planets |  mars    |  red
    

    loop over all the rows:

    • when A.a changes, output the title
    • then always output the B.b + B.a value

    pseudo code for application calling SQL:

    set last_A = null
    exec query
    
    loop over result set {
        if last_A == null or fetch_A!=last_A {
            last_A=fetch_A
            display fetch_a
        }
        display fetch_Bb + fetch_Ba
        }
    }//loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 96k
  • Answers 96k
  • 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 If the web service being invoked uses windows integrated security,… May 11, 2026 at 7:12 pm
  • Editorial Team
    Editorial Team added an answer It sounds like you're confusing two different things. Windows authentication… May 11, 2026 at 7:12 pm
  • Editorial Team
    Editorial Team added an answer For hovering effects, jQuery provides the hover() pseudo-event, which behaves… May 11, 2026 at 7:12 pm

Related Questions

I've worked with MySQL and MSSQL for some time and have used a variety
I'm curious How do you people think ? (I mean a way of thinking)
I'm working on a large scale performance critical asp web application with a pretty
Almost 5 years ago Joel Spolsky wrote this article, The Absolute Minimum Every Software
I asked a question about How to read a text file reversely with iterator

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.