文章目录
数据库查询,并且sql直接返回json格式
sql
SELECT
JSON_ARRAYAGG(
JSON_OBJECT(
'groupId', g.id,
'groupName', g.NAME,
'groupColor', g.group_color,
'num', COALESCE(c.num, 0),
'type', g.type
)
) AS result
FROM
xbd_group g
LEFT JOIN (
SELECT
group_id,
COUNT(id) AS num
FROM
xbd_customer_1
WHERE
y_uid = 1
GROUP BY
group_id
) c ON g.id = c.group_id
WHERE
g.y_uid IN (0, 1)
AND g.STATUS = 0
ORDER BY
g.id
LIMIT 10
OFFSET 0;
返回值:
json
[
{
"num": 2,
"type": 1,
"groupId": 1,
"groupName": "手机通讯录",
"groupColor": null
},
{
"num": 31,
"type": 4,
"groupId": 4,
"groupName": "未分组",
"groupColor": null
}
]