2016-12-15 55 views
1

我有3个表是这样的:MySQL的计数逗号分隔

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 | 
+------+----------+---------------+-------------------+-------------------+ 

我知道如果数据库的设计正确,这个问题将会变得微不足道,但我们现在还没有奢侈的应用程序重写。

任何帮助将不胜感激。

+1

查找'group_concat' –

回答

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

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用于设置模式)