2016-01-22 141 views
1

我已經從一個月的表數據編碼json數組,如下所示,但如何組合所有數據後,如何將多個數據表與其他月份在一個json數組和GROUP BY部分組合?如何合併多個表數據並將json編碼爲一個數組?

<?php 

include("dbconfig.php"); 

$sql = "SELECT dept, SUM(ttlot) AS sum_ot, SUM(ttlnorm) AS sum_norm 
     FROM month_jan 
     GROUP BY dept 
     ORDER BY sum_ot DESC"; 

$result = mysqli_query($dbconfig, $sql) or die("Error in Selecting " . mysqli_error($dbconfig)); 

$category = array(); 
$category['name'] = 'Dept'; 

$series1 = array(); 
$series1['name'] = 'Normal'; 

$series2 = array(); 
$series2['name'] = 'OT'; 

$emparray = array(); 
while ($row = mysqli_fetch_assoc($result)) { 

$category['data'][] = $row['dept']; 
$series1['data'][] = $row['sum_norm']; 
$series2['data'][] = $row['sum_ot']; 

} 

$result = array(); 
array_push($result,$category); 
array_push($result,$series1); 
array_push($result,$series2); 

$json = json_encode($result,JSON_NUMERIC_CHECK); 

echo $json; 

mysqli_close($dbconfig); 
?> 

產出月月:

[{"name":"Dept","data":["CNC","MACH","ANOD","BUFF","CAST","POLISH","SL","EPT","TUMB","TOOL","SHOT","QC","LOG","MAIN","LC","WWT","OG","NPD","E-COAT","SFT"]},{"name":"Normal","data":[47429.1,39975.7,34553.8,49075.9,28316.3,21237.1,13492.5,5848.2,7691.1,6963.9,5636.1,7555.8,5821.9,2161.2,1812,1191.7,1479.1,1299.6,11542.6,602]},{"name":"OT","data":[20041,17874,14431,13535.5,8800.5,5613.5,3569.5,3101,2327,2278,2237,2142,1810,942,690,456,297,110.5,66,50.5]}] 

什麼結果,我結合四個月後,想:

[{"name":"Month","data":["Jan","Feb","Mac","Apr"]},{"name":"Normal","data":[504291,409757,295538,430759]},{"name":"OT","data":[89041,96874,81431,80535]}] 

有誰可以幫我解決這個問題?

回答

0

隨着MySQL的JSON功能和GROUP_CONCAT的組合,你可以做一些非常真棒事情結合表:

你期望的結果:

[{"name":"Month","data":["Jan","Feb","Mac","Apr"]},{"name":"Normal","data":[504291,409757,295538,430759]},{"name":"OT","data":[89041,96874,81431,80535]}] 

利用GROUP_CONCAT你不需要連接表,而只是一組由現場由您的領域聚集:

SELECT 
    CONCAT('{\"NAME\" : \"NORMAL\",', 
    '\"data\": [', GROUP_CONCAT('"', SUM(ttlnorm), '"'), ']},', 
    '{\"NAME\" : \"OT\",', 
    '\"data\": [', GROUP_CONCAT('"', SUM(ttlot), '"'), ']}', 
) 
    from month_jan 
    GROUP BY dept; 

如果您使用的是一個月,存儲(「month_jan」)了,你會表結構我需要在你的月份之間進行聯合或者改變你的表格結構,以便所有需要的月份都包含在同一個表格中。您可以根據您的GROUP BY在多個級別上執行拼接。舉例來說,如果你有兩個組的領域,你將能夠巢您的JSON: 例如:GROUP BY DEPT,MONTH

SELECT 
    CONCAT('{', 
    '\"DEPT\" :\"', dept,'\",', 
    '{\"NAME\" : \"Month\",', 
    '\"data\": [', GROUP_CONCAT('"', month, '"'), ']},', 
    '{\"NAME\" : \"NORMAL\",', 
    '\"data\": [', GROUP_CONCAT('"', SUM(ttlnorm), '"'), ']},', 
    '{\"NAME\" : \"OT\",', 
    '\"data\": [', GROUP_CONCAT('"', SUM(ttlot), '"'), ']}', 
) 
    from ttl_ot_norm 
    GROUP BY dept, month; 

其結果是,在您GROUP_CONCAT的數據將根據制定出你表達的形式。

另一種解決方案是創建一個可以更新基於cron作業或表觸發和這個數據放入一個JSON字段類型所以它不斷地準備好與便宜的CPU成本檢索表。這樣,每個查詢就不會有連接和表連接的額外開銷。

您可以通過梳理GROUP_CONCAT和分組你的表跳過了很多你的PHP聚集。多年來,我已經節省了數百小時的編程和MySQL的JSON特性的組合(下面的鏈接)

版本8引入了這些功能,您不需要使用JSON_ARRAYAGG創建自己的JSON其他很棒的JSON功能。儘管如此,以上版本將適用於版本5及更高版本。

https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat

https://dev.mysql.com/doc/refman/5.7/en/json-creation-functions.html#function_json-array

VERSION 8: https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat

相關問題