0

Is it possible in MySQL to query from a database and get the result as Array?

QUERY

SELECT foo.name,
(
   SELECT data 
   FROM bar 
   WHERE id=foo.userid
) data  
FROM table1 foo;

Expected Output

+---------+------------------------+
| name    |  data                  |
+=========+========================+
| john    |  ['one','two','three'] |
| martha  |  ['one','five','nine'] |
+---------+------------------------+

thanks!

5
  • One array, or an array per row? What should the array look like? The output you specify doesn't make that clear.
    – GolezTrol
    Sep 22, 2014 at 8:31
  • possible duplicate of SQL Results as PHP Array
    – GolezTrol
    Sep 22, 2014 at 8:31
  • I believe this is out of the scope of a classic relational DBMS, research for ORM (Object Relational Mapping) frameworks (e.g., Hibernate), they should help you with this task. Sep 22, 2014 at 8:32
  • no, it must be pure sql, no php involve Sep 22, 2014 at 8:32
  • Array representation is different in some languages. You need to specify your need exactly to get help.
    – mrcrgl
    Sep 22, 2014 at 9:02

1 Answer 1

2

You can use GROUP_CONCAT

SELECT foo.name,(SELECT GROUP_CONCAT(data SEPARATOR ',') 
                   FROM bar 
                  WHERE id=foo.userid
               GROUP BY data) AS data 
  FROM table1 foo;

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.