sql查询,直接返回json数组格式

文章目录

数据库查询,并且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
    }
]
相关推荐
jason成都几秒前
实战 | 国产数据库 R2DBC-JDBC 桥接踩坑记 - JetLinks适配达梦数据库
java·数据库·物联网
Elastic 中国社区官方博客3 分钟前
使用 Elasticsearch 管理 agentic 记忆
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
小宇的天下9 分钟前
Calibre 3Dstack --每日一个命令day13【enclosure】(3-13)
服务器·前端·数据库
云和数据.ChenGuang18 分钟前
达梦数据库安装服务故障四
linux·服务器·数据库·达梦数据库·达梦数据
wtsolutions44 分钟前
Understanding JSON Formats - What JSON to Excel Supports
json·excel
wtsolutions1 小时前
Advanced Features - Unlocking the Power of JSON to Excel Pro
linux·json·excel
尽兴-1 小时前
MySQL 8.0主从复制原理与实战深度解析
数据库·mysql·主从复制
Mr_sun.1 小时前
Day04——权限认证-基础
android·服务器·数据库
百锦再1 小时前
国产数据库现状与技术演进
数据库·python·plotly·flask·virtualenv·pygame·tornado
煎蛋学姐1 小时前
SSM学生会综合管理系统8berj(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·计算机毕业设计·ssm 框架·学生会综合管理系统