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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:06:17+00:00 2026-05-11T00:06:17+00:00

Coming from another question of mine where I learnt not to EVER use db

  • 0

Coming from another question of mine where I learnt not to EVER use db queries within loops I consequently have to learn how to fetch all the data in a convenient way before I loop through it.

Let’s say I have two tables ‘scales’ and ‘items’. Each item in items belongs to one scale in scales and is linked with a foreign key (scaleID). I want to fetch all that data into an array structure in one query such that the first dimension are all the scales with all the columns and nested within, all items of one scale all columns.

Result would be something like that:

scale 1, scaleParam1, scaleParam2, ... ....item1, itemParam1, itemParam2, ... ....item2, itemParam1, itemParam2, ... scale 2, scaleParam2, scaleParam2, ... ....item1, itemParam1, itemParam2, ... ....item2, itemParam1, itemParam2, ... 

So far I’ve done mainly left joins for one-to-one relationships. This is a one-to-many and I just can’t wrap my mind around it.

Is it a right join, could it also be done with a subquery, how to get the full outer rows into it as well…

later I would like to iterate through it with to nested foreach loops.

Maybe it’s just that I have a headache…

  • 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-11T00:06:17+00:00Added an answer on May 11, 2026 at 12:06 am

    The query should look something like this:

    SELECT * FROM scales INNER JOIN items ON scales.id = items.scale_id 

    If you want to iterate through with nested loops, you’ll need to pull this data into an array – hopefully you’re not pulling back so much that it’ll eat up too much memory.

    $scales = array();  while ($row = mysql_fetch_assoc($data)) {     if (!isset($scales[$row['scale_id']]))     {         $row['items'] = array();         $scales[$row['scale_id']] = $row;     }      $scales[$row['scale_id']]['items'][] = $row; } 

    Then you can loop through:

    foreach ($scales as $scale) {     foreach ($scale['items'] as $item)         ; //... do stuff } 

    Note: this is somewhat naive in that $scale and $item will both contain fields from BOTH tables… if that’s a problem then you need to change the assignments in the loop above to pull out only the fields you want.

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

Sidebar

Related Questions

Another question about my implementation. I am fairly new to java/android coming from a
This question is an extension from another question of mine @ Need some advice
I am using large random numbers as keys (coming in from another system). Inserts
Coming from a .NET background I'm use to reusing string variables for storage, so
I am coming back from after reading this c-faq question I am totaly confused
I Have a set of choices(options) coming from Database(around 36 records) which I have
I just started learning C++(coming from Java & Python) and am trying to learn
I'm new to the 4.0 framework (coming from 2.0), and I'm not entirely sure
I have about 20,000 records (coming from an SQLite db) that I need to
I have denormalized data (coming from a file) that needs to be imported into

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.