doujing6053 2013-09-10 16:37
浏览 9
已采纳

阵列数据计算

I have met problem dealing with multidimensional array. Below are the code to store all the data query from mysql to array. May I know is there any idea how to calculate how many different module name in the multidimensional array and break the status into 2 column which are late and present? The SQL query get the data from a complex table which store all ModuleName and different status. I need to categories them and show in a table. So basically I need to check how many module in the array and how many late or present for the module.

while($array = mysql_fetch_array($result)){
        $list[] = array(
                        "ModuleName"=>$array['ModuleName'],
                        "status"    =>$array['Status']
            ); 
    }

Sample of the SQL query

This is the sample of expected result

Sample Expected Result

  • 写回答

1条回答 默认 最新

  • dongtun2572 2013-09-10 16:44
    关注

    Updated to group by ModuleName

    SELECT ModuleName
          ,COUNT(*) AS module_count
          ,SUM(CASE WHEN Status != 'Present' THEN 1 ELSE 0 END) as late_count
          ,SUM(CASE WHEN Status  = 'Present' THEN 1 ELSE 0 END) as present_count
      FROM ( your original query )
     GROUP BY ModuleName
    

    Just replace "your original query" with the SQL you used to retrieve the data in the array.

    Original

    SELECT COUNT(*) AS module_count
          ,SUM(CASE WHEN Status != 'present' THEN 1 ELSE 0 END) as late_count
          ,SUM(CASE WHEN Status  = 'present' THEN 1 ELSE 0 END) as present_count
      FROM ( your original query )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度