1

We have a table.

customer         
+----+------+------+-------+
| id | type | name | group |
+----+------+------+-------+
| 1  | boy  | dave | adult |
| 2  | girl | lisa | child |
| 3  | girl | amy  | adult |
| 4  | boy  | ivan | child |
+------------------+-------+

If we query this table with

SELECT * FROM `customer` WHERE type = 'girl'

Result output

Array
(
    [0] => Array
        (
            [id] => 2
            [type] => girl
            [name] => lisa
            [group] => child
        )
    [1] => Array
        (
            [id] => 3
            [type] => girl
            [name] => amy
            [group] => adult
        )
)

I would like to receive the output like the following.

Array
(
    [girl] => Array
        (
        [child] => Array
            (
                [id] => 2
                [name] => lisa
            )
        [adult] => Array
            (
                [id] => 3
                [name] => amy
            )
        )
)

I could manipulate the result with another programming language, but wondered if this is possible with pure SQL?

Thanks in advance.

3
  • What are you trying to accomplish with this? Most of the reporting products I have used have drill down capabilities that could arrange the data like you ask for but from straight SQL i believe Drew is right. Commented Jan 21, 2014 at 22:49
  • I'm trying to accomplish less code overall, If it is not possible, I will have to manipulate the result after its returned.
    – cecilli0n
    Commented Jan 22, 2014 at 0:12
  • a language other than SQL (Java, C#, ETC...) would be better at handling your complex data structure. Commented Jan 23, 2014 at 19:24

1 Answer 1

1

As far as I know, no SQL has yet been designed to return associative arrays, but I could be wrong!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.