数据库字段(GROUP_CONCAT)作为数组 [英] db field(GROUP_CONCAT) as array
问题描述
我的表..
文章字段:
- 标识
- 名称
猫字段:
- 标识
- 名称
article_cats 字段:
- 标识
- 的article_id
- CAT_ID
我有这样的SQL语句:
I have this sql:
SELECT
a.id AS article_id ,
a.name AS article_name ,
COUNT( c.id ) AS num_categories ,
GROUP_CONCAT( c.name ) AS categorie_names
FROM
articles AS a
LEFT JOIN
article_cats AS ac
ON
( a.id = ac.article_id )
LEFT JOIN
cats AS c
ON
( ac.cat_id = c.id )
GROUP BY
a.id
和它提供此表的结构:
我需要的是,要获取数组类标识和名称,以便categorie_names将与领域CAT_ID和cat_name为每个类别的数组。
what i need is, to get categories id and name in array, so categorie_names will be an array with fields cat_id and cat_name for each category.
这可能与MySQL?
推荐答案
在数据库阵列意味着表。如果你想在阵列中的数据格式存储在一个单独的临时表中的值的最佳方式。
In database array means tables. If you want data in array format the best way to store the values in a seperate temporary table.
如同
article_id, category_id, category_name
1 1 Cat1
2 1 Cat1
3 2 Cat2
3 1 Cat1
或者你必须使用GROUP_CONCAT内的串联
Or you have to use a concatenation inside the group_concat
GROUP_CONCAT(cast(concat(c.id,\': \',c.name) AS char)SEPARATOR \', \') AS categorie_names
那么结果就会像2:Cat2,1:CAT2。您可以拆分(先用','然后':')。此值并检索ID和姓名
So the result will be like 2:Cat2,1:Cat2. You can split(first with ',' then ':') this value and retrieve ID and Name.
这篇关于数据库字段(GROUP_CONCAT)作为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!