之前我使用Ajax绘制谷歌图表(饼图),图表可以显示,因为它只涉及2列。
现在,我想绘制一个像这样的柱状图。
这是我的SQL查询:
$sql = "Select count(application_id) as count, application_status, category_group from mdl_local_jobs_application
inner join mdl_local_jobs_job on mdl_local_jobs_application.application_jobid = mdl_local_jobs_job.job_id
left join mdl_local_jobs_category on mdl_local_jobs_job.job_categoryid = mdl_local_jobs_category.category_id
left join mdl_cohort_members on mdl_local_jobs_application.application_applicantid = mdl_cohort_members.userid
left join mdl_local_cohortrole on mdl_cohort_members.cohortid = mdl_local_cohortrole.cohortid
left join mdl_role on mdl_local_cohortrole.roleid = mdl_role.id where shortname = 'graduates'
group by application_status, category_group";
使用以下代码将查询编码为Json输出:
$result_rows = mysql_num_rows($query);
$row_num = 0;
//loop fetching the query into json format
while ($result = mysql_fetch_array($query))
{
$row_num++;
if ($row_num == $result_rows){
echo "{\"c\":[{\"v\":\"" . $result['category_group'] . "\",\"f\":null},{\"v\":\"" . $result['application_status'] . "\",\"f\":null},{\"v\":" . $result['count'] . ",\"f\":null}]},";
} else {
echo "{\"c\":[{\"v\":\"" . $result['category_group'] . "\",\"f\":null},{\"v\":\"" . $result['application_status'] . "\",\"f\":null},{\"v\":" . $result['count'] . ",\"f\":null}]},";
}
}
echo " ] }";
这是输出:
{ "cols": [ {"id":"","label":"CATEOGRY","pattern":"","type":"string"}, {"id":"","label":"CATEOGRY","pattern":"","type":"number"}, {"id":"","label":"TOTAL","pattern":"","type":"number"} ], "rows": [ {"c":[{"v":"Accounting/Finance","f":null},{"v":"0","f":null},{"v":2,"f":null}]},{"c":[{"v":"Engineering","f":null},{"v":"0","f":null},{"v":1,"f":null}]}, ] }
但是,图表无法正确显示,因为应用程序状态的值不是数字。我确实阅读了其他解决方案,以便通过sql中的sum函数得到结果,但我不知道该怎么做。
我的问题:我想如何进行SQL查询以获取每个应用程序状态的总计数(0,1,2 - 状态类型)
我的预期输出是这样的:
{ "cols": [ {"id":"","label":"CATEOGRY","pattern":"","type":"string"}, {"id":"","label":"CATEOGRY","pattern":"","type":"number"}, {"id":"","label":"TOTAL","pattern":"","type":"number"} ], "rows": [ {"c":[{"v":"Accounting/Finance","f":null},{"v":4,"f":null},{"v":2,"f":null}]},{"c":[{"v":"Engineering","f":null},{"v":7,"f":null},{"v":1,"f":null}]}, ] }
区别在于第二个数组值,我希望通过使用SQL查询(使用求和函数)获得值,以获得0状态,1状态和2状态的每个状态的计算总和。
{"c":[{"v":"Accounting/Finance","f":null},{"v":4,"f":null},{"v":2,"f":null}]},
如果我进行此查询(在where语句中更改,我只能获得状态“0”的计数。我如何在同一查询中添加另一个where语句以获取其他状态的计数,状态为“1” “和状态”2“?
$sql = "Select count(application_id) as count, category_group from mdl_local_jobs_application
inner join mdl_local_jobs_job on mdl_local_jobs_application.application_jobid = mdl_local_jobs_job.job_id
left join mdl_local_jobs_category on mdl_local_jobs_job.job_categoryid = mdl_local_jobs_category.category_id
left join mdl_cohort_members on mdl_local_jobs_application.application_applicantid = mdl_cohort_members.userid
left join mdl_local_cohortrole on mdl_cohort_members.cohortid = mdl_local_cohortrole.cohortid
left join mdl_role on mdl_local_cohortrole.roleid = mdl_role.id where shortname = 'graduates' and application_status = 0
group by category_group";
答案 0 :(得分:0)
我尝试了这个SQL查询:
$sql = "Select count(application_id) as count, sum(case when application_status = 0 then 1 else 0 end) PendingCount, sum(case when application_status = 1 then 1 else 0 end) RejectedCount, sum(case when application_status = 2 then 1 else 0 end)SuccessCount, category_group from mdl_local_jobs_application
inner join mdl_local_jobs_job on mdl_local_jobs_application.application_jobid = mdl_local_jobs_job.job_id
left join mdl_local_jobs_category on mdl_local_jobs_job.job_categoryid = mdl_local_jobs_category.category_id
left join mdl_cohort_members on mdl_local_jobs_application.application_applicantid = mdl_cohort_members.userid
left join mdl_local_cohortrole on mdl_cohort_members.cohortid = mdl_local_cohortrole.cohortid
left join mdl_role on mdl_local_cohortrole.roleid = mdl_role.id where shortname = 'graduates'
group by category_group";
这是输出:
{ "cols": [ {"id":"","label":"CATEOGRY","pattern":"","type":"string"}, {"id":"","label":"PENDING","pattern":"","type":"number"}, {"id":"","label":"REJECTED","pattern":"","type":"number"}, {"id":"","label":"SUCCESS","pattern":"","type":"number"}, {"id":"","label":"TOTAL","pattern":"","type":"number"} ], "rows": [ {"c":[{"v":"Accounting/Finance","f":null},{"v":2,"f":null},{"v":0,"f":null}, {"v":0,"f":null},{"v":2,"f":null}]},{"c":[{"v":"Engineering","f":null},{"v":1,"f":null},{"v":0,"f":null}, {"v":0,"f":null},{"v":1,"f":null}]}, ] }
图表可以根据状态类型显示..
无论如何,感谢@Alex,@ Oleg的评论和分享,让我从问题中解脱出来。我很抱歉让你对我的问题感到困惑。