我已经从一个月的表数据中编码了json数组,如下所示,但是如何在组合所有数据后将多个数据表与其他月组合在一个json数组和GROUP BY部门中?
<?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);
?>
1月份的产出:
[{"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 :(得分:1)
我通过打印它们来手动完成,如果您在对两个表的JSON进行编码时遇到问题,我认为这是一个替代解决方案。 有时,具有相同名称的行会在 json_encode()函数上产生错误。
a subclass of C
?>
如果您的行具有相同的名称,则应像在上面的示例中那样重命名另一个(questionaire_name,questionaire_description)。
它将产生以下输出:
<?php
session_start();
require("../config.php");
$output = array();
$id = $_GET['id'];
if(isset($_SESSION['user'])){
$f_data ='';
$sql = "SELECT * FROM quiz WHERE subject_id=$id ORDER BY id DESC";
$query=$conn->query($sql);
while($row=$query->fetch_array()){
$sql2 = "SELECT * FROM q_details WHERE id=$row[2] ORDER BY id DESC LIMIT 1";
$query2=$conn->query($sql2);
while($row2=$query2->fetch_array()){
$f_data .= '{
"id":"'.$row[0].'",
"subject_id":"'.$row[1].'",
"questionaire_id":"'.$row[2].'",
"name":"'.$row[3].'",
"description":"'.$row[4].'",
"start":"'.$row[5].'",
"end":"'.$row[6].'",
"date":"'.$row[7].'",
"questionaire_name":"'.$row2[2].'",
"questionaire_description":"'.$row2[3].'"
},';
}
}
$f_data = substr($f_data, 0, -1);
echo '['.$f_data.']';
}
else{
echo"<script>window.open('http://localhost/lnhs_app/#/','_self')</script>";
}
答案 1 :(得分: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
版本8: https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat