mysql 暴库 group_concat(),数据库字段(GROUP_CONCAT)作为数组

MY tables..

articles fields:

id

name

cats fields:

id

name

article_cats fields:

id

article_id

cat_id

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

and it provide this table structure:

utsSw.png

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.

Is this possible with 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.

Like

article_id, category_id, category_name

1 1 Cat1

2 1 Cat1

3 2 Cat2

3 1 Cat1

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

So the result will be like 2:Cat2,1:Cat2. You can split(first with ',' then ':') this value and retrieve ID and Name.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值