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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:53:46+00:00 2026-05-31T18:53:46+00:00

I am having trouble getting well formatted results from my mysql query. Here is

  • 0

I am having trouble getting well formatted results from my mysql query.

Here is the database:

customers table 
id  name
1   Tim
2   Lauren
3   Prada

tanks table
id  c_id    location
1   1   Living Room
2   2   Living Room
3   2   Kitchen
4   3   Bedroom
5   3   Hallway

fish table
id  t_id    color
1   1   red
2   1   blue
3   2   white
4   2   green
5   2   black
6   3   red
7   3   orange
8   4   white
9   4   blue
10  5   black

Here is my query:

SELECT * FROM customers
LEFT JOIN tanks ON (tanks.c_id = customers.id)
LEFT JOIN fish ON (fish.t_id = tanks.id)
GROUP BY customers.id, tanks.id;

Here is the result that I am getting:

id  name    id  c_id    location    id  t_id    color
1   Tim 1   1   Living Room 1   1   red
2   Lauren  2   2   Living Room 3   2   white
2   Lauren  3   2   Kitchen 6   3   red
3   Prada   4   3   Bedroom 8   4   white
3   Prada   5   3   Hallway 10  5   black

Here is the format that I am trying to get:

  [0] => Array
    (
      [customers] => Array
        (
          [0] => Array
            (
              [id] => 2
              [title] => Lauren
              [tanks] => Array
                (
                  [0] => Array
                    (
                      [id] => 2
                      [location] => Living Room
                      [fish] => Array
                        (
                          [0] => Array
                            (
                              [id] => 3
                              [color] => white
                            )
                          [1] => Array
                            (
                              [id] => 4
                              [color] => green
                            )
                          [2] => Array
                            (
                              [id] => 5
                              [color] => black
                            )
                        )
                      )
                    [1] => Array
                      (
                        [id] => 3
                        [location] => Kitchen
                        [fish] => Array
                          (
                            [0] => Array
                              (
                                [id] => 6
                                [color] => red
                              )
                            [1] => Array
                              (
                                [id] => 7
                                [color] => orange
                              )
                          )
                      )
                )
            )
        )
    )

Can I get the desired result from one query or do I need to do multiple queries and then join them together in PHP?

Any help would be appreciated!

  • 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-31T18:53:47+00:00Added an answer on May 31, 2026 at 6:53 pm

    You can’t get this output directly from mysql because it always returns row based outputs. Bu after you get the result, you can convert the row-type array to your desired array with a bridge method.

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

Sidebar

Related Questions

I'm having trouble getting the results I want from a Salesforce/Apex/SOQL query. I want:
I'm having trouble when I pull an object from the database with getting the
I'm trying to get a query from a specific model. I'm having trouble getting
I'm having trouble getting the correct query (oracle) for what I'm looking for. Essentially
I am having trouble getting a working example that reads metadata from a WebVTT
I'm having some trouble getting two different SSH keys/GitHub accounts to play well together.
I'm having trouble getting a rotary encoder to work properly with AVR micro controllers.
I've been having trouble getting my ASP.NET application to automatically log users into the
I'm having trouble getting the following to work in SQL Server 2k, but it
I'm having trouble getting the right number of elements in the ArrayList alt in

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.