【问题标题】:Count all connected users on every department统计每个部门的所有连接用户
【发布时间】:2015-09-15 02:30:40
【问题描述】:

我有这个tellers,每个service departments上都可以看作users

我有这段代码,除了 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

它输出这个

这是我完整的数据库架构

正如您在我的teller_info table 的数据库架构中看到的那样,我有字段status,它是ENUM [Connected, Disconnected],如果teller_1Cashier 下,这是我想要获取的字段并且它是 Connected 它应该输出 active_teller in service_name Cashier is 1

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

service_info.active_tellers AS active_teller 

我试过了

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

但是输出不正确

编辑:

输出

show create table service_info

CREATE TABLE `service_info` (
  `service_id` int(11) NOT NULL AUTO_INCREMENT,
  `service_num` int(2) unsigned zerofill NOT NULL,
  `name` varchar(50) DEFAULT NULL,
  `starting_num` int(4) unsigned zerofill NOT NULL DEFAULT '0000',
  `ending_num` int(4) unsigned zerofill NOT NULL DEFAULT '0000',
  `current_serving` int(4) unsigned zerofill NOT NULL DEFAULT '0000',
  `last_printed` int(4) unsigned zerofill NOT NULL DEFAULT '0000',
  `remaining_queue` int(10) NOT NULL DEFAULT '0',
  `active_tellers` int(5) NOT NULL DEFAULT '0',
  PRIMARY KEY (`service_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 COMMENT='Table for service (Cashier, information, repair)'

【问题讨论】:

  • 添加了enums标签
  • 能否提供show create table service_info的输出

标签: mysql join enums count


【解决方案1】:

尝试计算服务中连接的柜员

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

【讨论】:

  • 这看起来不错,但在我应用它之后,它的结果是数量过多。我只有 1 个活跃的出纳员,但上面写着 15 个。
【解决方案2】:
(SELECT COUNT(*) FROM teller_info WHERE teller_info.`status` = "Connected" AND teller_info.service_id = service_info.service_id) AS active_teller

似乎对我有用。

【讨论】:

    猜你喜欢
    • 2015-04-28
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2012-05-26
    相关资源
    最近更新 更多