SUM IF条件真实回波描述

时间:2013-02-12 21:13:06

标签: php mysql

详情:

lo means layout
lo_act_id ( activity id ), lo_time ( time spent ) , up_time and down_time , lo_description , lo_client_id

如果活动ID<到99表示正常运行时间

如果活动ID>到99表示停机时间,但我已停机,我希望看到它的注意事项。

这是我使用的查询效果很好,但我不知道如何拍摄并显示lo_description

SELECT * , 
  SUM( IF( `lo_act_id` > 99 , lo_time , 0 )) AS down_time,
  SUM( IF( `lo_act_id` < 99 , lo_time , 0 ,lo_desc )) AS up_time
FROM tbl_Layout
LEFT  JOIN tbl_Client ON lo_client_id=client_code
GROUP BY lo_client_id

这就是我得到的

Client   Spent Time  Downtime
1        216           3 
20       250           0 
16       10           16 
35       60            0
60       30            0

这是我想要的:

Client    Spend Time    Downtime    Note
1         216           3           Replace Network Card
20        250           0     
16        10            16          Replace Main Board
                                    Monthly Maintenance
                                    Hurricane affect facilities
35        60            0
60        30            0

1 个答案:

答案 0 :(得分:0)

使用CAST摆脱“最小”和“分钟”(假设此字段始终包含分钟,而不是秒或小时)。

使用GROUP_CONCAT将描述粘贴在一起。

SELECT
  tbl_Client.*,
  SUM(IF(`lo_act_id` > 99, CAST(lo_time AS UNSIGNED), 0)) AS down_time,
  SUM(IF(`lo_act_id` < 99, CAST(lo_time AS UNSIGNED), 0)) AS up_time,
  GROUP_CONCAT(IF(`lo_act_id` > 99, lo_description, NULL) SEPARATOR "\n") AS note
FROM tbl_Layout
LEFT JOIN tbl_Client ON lo_client_id=client_code
GROUP BY lo_client_id