Mysql 连接和 group_concat

Mysql concat and group_concat

我正在尝试提出一个 sql 查询来显示客户信息及其订单。

这是想要的结果:

{
    "success": true,
    "client": {
        "name": "General Kenobit",
        "email": "test@test.com",
        "contact": 123456789,
        "registerDate": "2022-04-06T16:00:05.000Z",
        "status": "activo",
        "orders": [
            {
                "orderId": 1002,
                "total": 19.5,
                "payment": "money",
                "products": [
                    {
                        "productId": 1,
                        "product": "Test",
                        "quantity": 4
                    }
                ]
            },
            {
                "orderId": 1006,
                "total": 67.5,
                "payment": "money",
                "products": [
                    {
                        "productId": 1,
                        "product": "Test",
                        "quantity": 4
                    }
                    {
                        "productId": 2,
                        "product": "Product 2",
                        "quantity": 3
                    }
                ]
            },
            {
                "orderId": 1009,
                "total": 134,
                "payment": "card",
                "products": [
                    {
                        "productId": 1,
                        "product": "Test",
                        "quantity": 4
                    }
                    {
                        "productId": 2,
                        "product": "Product 2",
                        "quantity": 4
                    }
                    {
                        "productId": 3,
                        "product": "Food",
                        "quantity": 5
                    },

                ]
            }
        ]
    }
}

这是我要解决的问题

SELECT c.name, c.email, c.contact, c.registerDate, c.status,
CONCAT('[',
    GROUP_CONCAT(JSON_OBJECT("orderId", o.orderId, "total", o.total, "payment", o.payment, "products",
        CONCAT('[', GROUP_CONCAT(JSON_OBJECT("productId", p.productId, "product", p.product, "quantity", op.quantity) SEPARATOR ','), ']')
    ) SEPARATOR ','),
']') AS 'orders'
FROM t_client AS c
INNER JOIN t_order AS o ON o.email = c.email
INNER JOIN t_orderproduct AS op ON op.orderId = o.orderId
INNER JOIN t_product AS p ON p.productId = op.productId
WHERE c.clientId = 1
GROUP BY c.clientId

如果我在第二个 json_object 之前使用 group_concat 函数,我会收到错误 #1111,因为分组函数 (group) 的使用无效... 否则这就是它返回的结果:

{
    "success": true,
    "client": {
        "name": "General Kenobit",
        "email": "teste@teste.com",
        "contact": 123456789,
        "registerDate": "2022-04-06T16:00:05.000Z",
        "status": "activo",
        "orders": [
            {
                "orderId": 1002,
                "total": 19.5,
                "payment": "money",
                "products": [
                    {
                        "productId": 1,
                        "product": "Test",
                        "quantity": 4
                    }
                ]
            },
            {
                "orderId": 1006,
                "total": 67.5,
                "payment": "money",
                "products": [
                    {
                        "productId": 1,
                        "product": "Test",
                        "quantity": 4
                    }
                ]
            },
            {
                "orderId": 1009,
                "total": 134,
                "payment": "card",
                "products": [
                    {
                        "productId": 1,
                        "product": "Test",
                        "quantity": 4
                    }
                ]
            },
            {
                "orderId": 1006,
                "total": 67.5,
                "payment": "money",
                "products": [
                    {
                        "productId": 2,
                        "product": "Product 2",
                        "quantity": 3
                    }
                ]
            },
            {
                "orderId": 1009,
                "total": 134,
                "payment": "card",
                "products": [
                    {
                        "productId": 2,
                        "product": "Product 2",
                        "quantity": 4
                    }
                ]
            },
            {
                "orderId": 1009,
                "total": 134,
                "payment": "card",
                "products": [
                    {
                        "productId": 3,
                        "product": "Food",
                        "quantity": 5
                    }
                ]
            }
        ]
    }
}

我已经颠倒了整个查询,不知道还有什么地方可以调整。 任何建议或提示表示赞赏。

您不能在查询中进行嵌套聚合,因此您需要在子查询中对订单产品进行聚合。

如果您 运行 至少是 5.7.22,则可以使用 JSON_ARRAYAGG() 而不是 CONCAT()GROUP_CONCAT()

SELECT c.name, c.email, c.contact, c.registerDate, c.status,
       JSON_ARRAYAGG(JSON_OBJECT("orderId", o.orderId, "total", o.total, "payment", o.payment, "products", op.products)) AS orders
FROM t_client AS c
INNER JOIN t_order AS o ON o.email = c.email
INNER JOIN (
      SELECT op.orderId, JSON_ARRAYAGG(JSON_OBJECT("productId", p.productId, "product", p.product, "quantity", op.quantity)) AS products
      FROM t_orderproduct AS op 
      INNER JOIN t_product AS p ON p.productId = op.productId
      GROUP BY op.orderId
) AS op ON op.orderId = o.orderId
WHERE c.clientId = 1
GROUP BY c.clientId