统计每个部门的所有连接用户

时间:2015-09-15 02:30:40

标签: mysql join enums count

我有这个tellersusers

可以被视为service departments

我有这个代码,除了active_tellers字段外,它会输出我想要的内容。

SELECT service_info.name AS service_name, 
service_info.current_serving AS current_service, 
service_info.last_printed AS last_printed, 
service_info.remaining_queue AS remaining_queue, 
AVG(teller_log.duration), 
service_info.active_tellers AS active_teller 
FROM user_info 
JOIN teller_info 
ON user_info.teller_id = teller_info.teller_id 
JOIN service_info 
ON service_info.service_id = teller_info.service_id 
JOIN teller_log 
ON user_info.user_id = teller_log.user_id 
GROUP BY 
service_info.name

输出此

enter image description here

这是我完整的数据库架构

enter image description here

正如您在teller_info table我的数据库架构中所看到的,我有字段status,而ENUM [Connected, Disconnected]这是我想要获取的字段,如果{{1}在teller_1下, Cashier 它应该在Connected输出active_teller service_name Cashier

我遇到问题的代码部分是这个

1

我试过

service_info.active_tellers AS active_teller 

但输出错误

enter image description here

修改:

的输出
(SELECT COUNT(teller_info.teller_id) FROM teller_info WHERE status = "Connected") as a

2 个答案:

答案 0 :(得分:1)

尝试计算服务中连接的出纳员

SUM(IF(teller_info.status = 'Connected', 1, 0)) AS active_tellers 

答案 1 :(得分:0)

(SELECT COUNT(*) FROM teller_info WHERE teller_info.`status` = "Connected" AND teller_info.service_id = service_info.service_id) AS active_teller

似乎在我的案例中工作。