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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:39:13+00:00 2026-05-26T19:39:13+00:00

I have no idea if this is possible but is there a way in

  • 0

I have no idea if this is possible but is there a way in MySQL to produce a single query where the multiple results of a one to many table join can be set as an array on a key of the result for the one item?

I realise that question isn’t very clear so I’ll explain what I’m after further:

Firstly, I’m currently using implicit joins and would like to learn more on explicit joins (of which I currently know very little), perhaps these could provide the answer I’m looking for?

For example given two tables:

CREATE TABLE `a` (
    `id_a` int(11) NOT NULL AUTO_INCREMENT,
    `a_column1` varchar(255) NOT NULL,
    ...
    PRIMARY KEY (`id_a`)
)

CREATE TABLE `b` (
    `id_b` int(11) NOT NULL AUTO_INCREMENT,
    `id_a` int(11) NOT NULL,
    `b_column1` varchar(255) NOT NULL,
    ...
    PRIMARY KEY (`id_b`)
)

Where table b has many entries related to a single entry in table a.

If I were to run the following query:

SELECT a.*, b.* FROM a, b WHERE b.id_a = a.id_a AND a.id_a = x;

I would get an array with multiple entries with the data of the single item id x repeated. What I actually want is a single row returned from table a with a key defined as b which contains an array of the multiple matching entries from table b. I suspect that this is not possible with a query alone, but it would be great if it was. Currently I am doing the following in PHP (where $this->_db is a Zend Framework database adapter). This runs a lot of queries!:

$query = "SELECT * FROM a WHERE id_a = ?";
$items = $this->_db->fetchAll($query, $id);

foreach($items as $key => $item) {
    $query = "SELECT * FROM b WHERE id_a = ?";
    $items[$key]['b'] = $this->_db->fetchAll($query, $item['id']);
}

Alternatively I can use my original join query and post process, which I suspect is more efficient, but means I need to explicitly copy over the columns I need (a pain and far from elegant):

$query = "SELECT * FROM a, b WHERE a.id_a = b.id_a AND a.id_a = ?";
$items = $this->_db->fetchAll($query, $id);
$output = array('a_column1' => $items[0]['a_column1'], etc...);
$output['b'] = array();

foreach($items as $item) {
    $b = array('b_column1' => $item['b_column1'], etc...);
    $output['b'][] = $b;
}
  • 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-26T19:39:14+00:00Added an answer on May 26, 2026 at 7:39 pm

    Your query that uses implicit JOIN:

    SELECT a.*
         , b.* 
    FROM a, b 
    WHERE b.id_a = a.id_a 
      AND a.id_a = x
    

    With explicit JOIN:

    SELECT a.*
         , b.* 
    FROM a
      JOIN b
        ON b.id_a = a.id_a 
    WHERE a.id_a = x
    

    One way to have the data in one query is to use the GROUP_CONCAT() function. But it may not be in a format you can use:

    SELECT a.*
         , GROUP_CONCAT( b.id_b
                         ORDER BY b.id_b ASC
                         SEPARATOR ',' 
                       ) AS b_ids
         , GROUP_CONCAT( b.b_column1 
                         ORDER BY b.id_b ASC
                         SEPARATOR ',' 
                       ) AS b_column1s
         , ...                            --- etc
    FROM a
      JOIN b
        ON b.id_a = a.id_a 
    WHERE a.id_a = x
    GROUP BY a.id_a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have no idea how this works or if it is even possible but
I have no idea if this is possible, but if it is, what would
I have no idea if this is possible ... but it would be cool.
I have no idea of this is actually possible, but i want the device
I don't think this is possible, but thought someone might have a nifty idea
I'm not sure if this is possible, but is there a way (pre-written library
I don't think this is possible, but I'll ask anyway... Is there some way
Is this possible at all? The idea is to have the following: if ($some_statement)
I have no idea if this question fit to SO, but if it doesn't
I have no idea why this doesn't work, but doing some validation functions and

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.