Mysql 5.1本机查询转换为嵌套的JSON数组

时间:2017-04-11 15:04:03

标签: php mysql json

我有以下两个表格;

默认值:

 Consultant  ¦ Company
-------------+----------- 
Consultant 1 ¦ Company 1  
Consultant 2 ¦ Company 1  
Consultant 3 ¦ Company 1  
Consultant 4 ¦ Company 2  
Consultant 5 ¦ Company 3  
Consultant 6 ¦ Company 4  

和GP:

Week Number ¦ Weekending ¦  Month  ¦ quarter ¦ Year  ¦ Consultant   ¦ Type ¦ Total
------------+------------+---------+---------+-------+--------------+------+-------  
    0       ¦ 01/01/2017 ¦ January ¦    1    ¦ 2017  ¦ Consultant 1 ¦ Temp ¦   450  
    0       ¦ 01/01/2017 ¦ January ¦    1    ¦ 2017  ¦ Consultant 1 ¦ Perm ¦  1000  
    0       ¦ 01/01/2017 ¦ January ¦    1    ¦ 2017  ¦ Consultant 2 ¦ Temp ¦     0  
    0       ¦ 01/01/2017 ¦ January ¦    1    ¦ 2017  ¦ Consultant 2 ¦ Perm ¦  2500  
    0       ¦ 01/01/2017 ¦ January ¦    1    ¦ 2017  ¦ Consultant 3 ¦ Temp ¦  5258  
    0       ¦ 01/01/2017 ¦ January ¦    1    ¦ 2017  ¦ Consultant 3 ¦ Perm ¦  5890  
    0       ¦ 01/01/2017 ¦ January ¦    1    ¦ 2017  ¦ Consultant 4 ¦ Temp ¦  8706  
    0       ¦ 01/01/2017 ¦ January ¦    1    ¦ 2017  ¦ Consultant 4 ¦ Perm ¦  6855  

我想要做的主要是查询GP表格,了解每家公司的成交量(按临时或烫发)以及每位顾问的贡献

[{
    "Company_Name" : "Company",
    "TotalGP" : "Total_GP",
    "Consultant_Contributions" : [{
            "Consultant_Name" : "Consultant",
            "Toal_GP" : "GP"
        }, {
            "Consultant_Name" : "Consultant",
            "Toal_GP" : "GP"
        }
    ]
}]

因为我对这种事情很陌生,所以我只能设法达到每个公司已经赚了多少,但不是JSON格式;

SELECT    sum(`Total`) as Total_GP , company    
FROM      `GP`
LEFT JOIN `Defaults` on GP.Consultant = Defaults.Consultant 
WHERE     quarter="1" AND type="temp"
GROUP BY  Company
ORDER BY  company ASC

这给了我这个:

  Total_GP ¦ company
-----------+----------
   1962.52 ¦ Company 1  
 101703.54 ¦ Company 3  
 378522.45 ¦ Company 2  

任何帮助都非常感激。

0 个答案:

没有答案