将sql结果作为Array获取

时间:2014-09-22 08:27:54

标签: mysql sql

MySQL是否可以从数据库进行查询并将结果作为数组?

QUERY

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

预期输出

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

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用GROUP_CONCAT

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