return group_concat data as array

2020-08-09 09:32发布

I want to return values I retrieve from the db using group_concat as an array of data. Is it possible to do this in the mysql query? Or do I need to explode the data into an array?

GROUP_CONCAT(sh.hold_id) as holds

returns this

[holds] => 3,4

I want it to return:

[holds] => array(3,4)

标签: php mysql
3条回答
forever°为你锁心
2楼-- · 2020-08-09 10:11

If you need to do it on the MySQL level, and then you may probably parse it to an object. You can do the following

SELECT CONCAT("[", GROUP_CONCAT(category.name), "]") AS categories
From categories
查看更多
地球回转人心会变
3楼-- · 2020-08-09 10:14

As I said in my comment: you need to explode the data into an array, using php code like this:

$holds = explode(',', $holds);

because mysql has no concept of array-type for data.

查看更多
Juvenile、少年°
4楼-- · 2020-08-09 10:24

MySQL has no concept of arrays. Therefore it is not able to return an array. It is up to your processing code (here the php scripts) to convert the concatenated notation into a php array.

查看更多
登录 后发表回答