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. Commented Sep 22, 2014 at 8:31
  • possible duplicate of SQL Results as PHP Array Commented 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. Commented Sep 22, 2014 at 8:32
  • no, it must be pure sql, no php involve Commented Sep 22, 2014 at 8:32
  • Array representation is different in some languages. You need to specify your need exactly to get help. Commented 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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.