如何使用Mysql Database CONCAT和Group Concat函数创建json数据

时间:2019-03-31 22:49:52

标签: mysql

Am试图通过利用三个数据库表的信息来创建json数组。

利用此处的信息和解决方案后 source 我开始意识到如何通过Mysql Concat和Group_Concat方法仅使用一个查询就可以提高应用程序性能。

三个表的创建过程如下,每个表都有其参考键

Create table categorys(id int primary key auto_increment, category_name varchar(30), buyer_userid int(30));

Create table product(id int primary key auto_increment, product_name varchar(30), buyer_userid int(30),category_id int,
foreign key (category_id) references categorys(id));


Create table sales(items_id int primary key auto_increment, item_name varchar(30),quantity varchar(30), buyer_userid int(30),
product_id int, foreign key (product_id) references product(id));

这是我期望的Json格式...

[
{"id":"1","cat_name":"Provision","catbuy_userid":"100",
"products_info":[{"productid":"2","product_name":"Malt","buyer_userid":"100",
"sales_Info":[{"items_id":"1","item_name":"malt","buyer_userid":"100","quantity":"72"}]
}]},
{"id":"2","cat_name":"Cosmetics","catbuy_userid":"200",
"products_info":[{"productid":"3","product_name":"soapy","buyer_userid":"200",
"sales_Info":[{"items_id":"2","item_name":"cream","buyer_userid":"200","quantity":"83"}]
}]}
]

到目前为止我的成功和问题。

我能够获取第一张表和第二张表(分别是类别表和产品表)的json记录。

当我尝试使用GROUP_CONCAT(CONCAT())方法获取第三张表(销售)的数据时,它显示错误无效使用组函数

这是到目前为止的实现。

 SELECT 
     CONCAT(
       "{"
     ,     '"id"' , ":" , '"' , categorys.id , '"' , ","
     ,     '"name"' , ":" , '"' , categorys.category_name , '"' , ","
     ,     '"productInfo"' , ":" , "["
                              ,GROUP_CONCAT(CONCAT(
"{"
     ,     '"pid"' , ":" , '"' , product.id , '"' , ","
     ,     '"productname"' , ":" , '"' , product.product_name , '"' , ","


     ,     '"SalesInfo"' , ":" , "["
                              , GROUP_CONCAT(CONCAT(
"{"
     ,     '"sales id"' , ":" , '"' , sales.items_id , '"' , ","
     ,     '"item name"' , ":" , '"' , sales.item_name , '"' , ","

  , "}"
))
                          , "]"



  , "}"
))
                          , "]"
     , "}"
     ) AS json 
  FROM 
   categorys 
  INNER JOIN 
    product 
  ON 
    categorys.id = product.category_id
  INNER JOIN
    sales 
  ON
    product.id = sales.product_id
  WHERE 
    categorys.id IN(SELECT id FROM categorys)
  GROUP BY
     categorys.id

1 个答案:

答案 0 :(得分:0)

您不能嵌套GROUP_CONCAT之类的分组功能。相反,您将需要在子查询中创建productsales JSON,然后将其包含到顶级JSON中。没有样本数据就很难确定,但是类似这样的方法应该起作用:

 SELECT 
     CONCAT(
       "{"
     ,     '"id"' , ":" , '"' , categorys.id , '"' , ","
     ,     '"name"' , ":" , '"' , categorys.category_name , '"' , ","
     ,     '"productInfo"' , ":" , "["
                              ,GROUP_CONCAT(product_sales)
                          , "]"
     , "}"
     ) AS json 
  FROM 
   categorys 
  INNER JOIN (SELECT CONCAT(
"{"
     ,     '"pid"' , ":" , '"' , product.id , '"' , ","
     ,     '"productname"' , ":" , '"' , product.product_name , '"' , ","


     ,     '"SalesInfo"' , ":" , "["
                              , GROUP_CONCAT(CONCAT(
"{"
     ,     '"sales id"' , ":" , '"' , sales.items_id , '"' , ","
     ,     '"item name"' , ":" , '"' , sales.item_name , '"' , ","

  , "}"
))
                          , "]"



  , "}"
) AS product_sales
             FROM product 
  INNER JOIN
    sales 
  ON
    product.id = sales.product_id) p
  ON 
    categorys.id = p.category_id
  WHERE 
    categorys.id IN(SELECT id FROM categorys)
  GROUP BY
     categorys.id