MySQL Count逗号分隔

时间:2016-12-15 13:35:16

标签: mysql sql mysqli

我有3张这样的表:

table_events

+------+----------+----------------------+
| ID   | Title    | Employees            |
+------+----------+----------------------+
| 1    | Event1   | john,james           |
+------+----------+----------------------+
| 2    | Event2   | sarah,jessica        |
+------+----------+----------------------+

table_check_in

+------+----------+----------+---------------------+
| ID   | Time     | EventID  | By                  |
+------+----------+----------+---------------------+
| 1    | 08:30    | 1        | john                |
+------+----------+----------+---------------------+
| 2    | 08:30    | 1        | james               |
+------+----------+----------+---------------------+
| 3    | 09:30    | 1        | john                |
+------+----------+----------+---------------------+
| 4    | 10:30    | 2        | sarah               |
+------+----------+----------+---------------------+
| 5    | 10:35    | 2        | sarah               |
+------+----------+----------+---------------------+

table_problems

+------+----------------+----------+---------------------+
| ID   | Comment        | EventID  | By                  |
+------+----------------+----------+---------------------+
| 1    | Broken door    | 1        | john                |
+------+----------------+----------+---------------------+
| 2    | Slippery floor | 1        | john                |
+------+----------------+----------+---------------------+
| 3    | Leaking tap    | 1        | john                |
+------+----------------+----------+---------------------+
| 4    | Broken window  | 2        | jessica             |
+------+----------------+----------+---------------------+
| 5    | Broken glass   | 2        | jessica             |
+------+----------------+----------+---------------------+

我想打印这样的东西:

+------+----------+---------------+-------------------+-------------------+
| ID   | Title    | Employees     | Count_Check_In    | Count_Problems    |
+------+----------+---------------+-------------------+-------------------+
| 1    | Event1   | john,james    | john:2,james:1    | john:3,james:0    |
+------+----------+---------------+-------------------+-------------------+
| 2    | Event2   | sarah,jessica | sarah:2,jessica:0 | sarah:0,jessica:2 |
+------+----------+---------------+-------------------+-------------------+

我知道如果数据库设计得当,这个问题会很简单,但我们目前还没有重写应用程序。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

您需要首先使用union从签入和问题表中获取每个事件ID的所有员工。

然后left join从每个签入和问题表到前一个结果的计数,以获得0计数。

最后使用group_concat为每个事件ID获取一行结果。

select te.id,te.title,te.employees
,group_concat(concat(t.`By`,':',coalesce(tccnt.cnt,0))) count_check_in
,group_concat(concat(t.`By`,':',coalesce(tpcnt.cnt,0))) count_problems
from table_events te 
left join (select eventid,`By` from table_check_in
           union
           select eventid,`By`from table_problems) t on te.id = t.eventid
left join (select eventid,`By`,count(*) cnt from table_check_in group by eventid,`By`) tccnt on tccnt.eventid = t.eventid and tccnt.`By`=t.`By`
left join (select eventid,`By`,count(*) cnt from table_problems group by eventid,`By`) tpcnt on tpcnt.eventid = t.eventid and tpcnt.`By`=t.`By`
group by te.id,te.title,te.employees

Sample Demo (感谢@valex设置架构)

答案 1 :(得分:1)

您可以使用GROUP_CONCAT获取结果。这是一个例子。唯一遗漏的是有0支票或问题的员工。

SELECT ID, Title,Employees,
       GROUP_CONCAT(DISTINCT CONCAT(check_in.`By`,':',check_in.cnt)) 
                   as Count_Check_In,
       GROUP_CONCAT(DISTINCT CONCAT(problems.`By`,':',problems.cnt)) 
                   as Count_Problems       
FROM table_events
LEFT JOIN (SELECT EventID,`By`, COUNT(*) as cnt 
        FROM table_check_in
        GROUP BY EventID,`By`) as check_in
      ON table_events.ID = check_in.EventID  
LEFT JOIN (SELECT EventID,`By`, COUNT(*) as cnt 
        FROM table_problems
        GROUP BY EventID,`By`) as problems
      ON table_events.ID = problems.EventID  
GROUP BY table_events.id

Demo