简体   繁体   English

Mysql concat 和 group_concat

[英]Mysql concat and group_concat

I'm trying to come up with a sql query that shows the client information as well as their orders.我正在尝试提出一个 sql 查询来显示客户信息及其订单。

this is the desired result:这是期望的结果:

{
    "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
                    },

                ]
            }
        ]
    }
}

and this is is query I'm trying to solve这是我要解决的问题

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

If I use the group_concat function before the second json_object I get error #1111 for invalid use of grouping function (group)... Otherwise this is what it comes back as result:如果我在第二个 json_object 之前使用 group_concat function,我会收到错误 #1111,因为无效使用分组 function(组)...否则这就是它返回的结果:

{
    "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
                    }
                ]
            }
        ]
    }
}

I turned the whole query upside down already and don't know where else to tweak.我已经把整个查询倒过来了,不知道还有什么地方可以调整。 Any suggestion or tip is appreciated.任何建议或提示表示赞赏。

You can't have nested aggregations in a query, so you need to do the aggregation of the order products in a subquery.您不能在查询中进行嵌套聚合,因此您需要在子查询中对订单产品进行聚合。

And instead of CONCAT() and GROUP_CONCAT() , you can use JSON_ARRAYAGG() if you're running at least 5.7.22.如果您至少运行 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM