Mysql不同表的不同计数。 1个查询中可以吗?

问题描述 投票:-1回答:1

我有这些表:

  • nms_alert
  • nms_alert_not_attend
  • nms_employee_log

如果我像这样加入他们:

SELECT DISTINCT 
    nms_alert.id, 
    nms_alert.alert_begin as dataAlert, 
    nms_employee_log.begin as employeeBegin,
    nms_employee_log.end as employeeEnd,
    nms_employee_log.zone_id,
    nms_employee_log.device_type,
    nms_employee_log.model,
    nms_employee_log.wifi_network
    FROM `nms_alert`  
    INNER JOIN nms_alert_not_attend on nms_alert.id = nms_alert_not_attend.nms_alert_id 
    INNER JOIN nms_employee_log on ( nms_employee_log.begin <= alert_begin AND nms_employee_log.end >= alert_begin )
    WHERE nms_alert_not_attend.not_attend_id = 3 
    AND nms_alert.user_attended_id=3 
    AND nms_employee_log.employee_id= 11 

正在提供:

id      dataAlert            employeeBegin       employeeEnd    zone_id device_type model   wifi_network    
62202   2019-12-13 20:31:20 2019-12-13 20:18:28 2019-12-13 20:46:42 27              
62202   2019-12-13 20:31:20 2019-12-13 20:18:28 2019-12-13 20:47:04 27              
62202   2019-12-13 20:31:20 2019-12-13 20:18:28 2019-12-13 20:47:04 25              
62202   2019-12-13 20:31:20 2019-12-13 20:18:28 2019-12-13 20:47:04 26              
62202   2019-12-13 20:31:20 2019-12-13 20:18:28 2019-12-13 20:47:04 24              
62202   2019-12-13 20:31:20 2019-12-13 20:18:28 2019-12-13 20:46:42 26              
62202   2019-12-13 20:31:20 2019-12-13 20:18:28 2019-12-13 20:46:41 24              
62202   2019-12-13 20:31:20 2019-12-13 20:18:28 2019-12-13 20:46:41 25              
55526   2019-12-03 19:11:53 2019-12-03 16:13:43 2019-12-03 19:40:59 24  samsung SM-G390F    wifi    
55526   2019-12-03 19:11:53 2019-12-03 16:13:43 2019-12-03 19:41:00 26  samsung SM-G390F    wifi    
55526   2019-12-03 19:11:53 2019-12-03 16:13:43 2019-12-03 19:40:59 25  samsung SM-G390F    wifi    
55526   2019-12-03 19:11:53 2019-12-03 16:13:43 2019-12-03 19:41:00 27  samsung SM-G390F    wifi    
53824   2019-11-28 20:36:02 2019-11-28 15:08:00 2019-11-29 14:28:07 25              
53824   2019-11-28 20:36:02 2019-11-28 15:08:00 2019-11-29 14:28:07 26              
53824   2019-11-28 20:36:02 2019-11-28 15:08:00 2019-11-29 14:28:07 24              
53579   2019-11-27 13:29:08 2019-11-27 07:42:24 2019-11-27 14:09:45 24  samsung SM-G390F    wifi    
53579   2019-11-27 13:29:08 2019-11-27 07:42:24 2019-11-27 14:09:45 25  samsung SM-G390F    wifi    
53579   2019-11-27 13:29:08 2019-11-27 07:42:24 2019-11-27 14:09:45 26  samsung SM-G390F    wifi    
53437   2019-11-26 17:49:58 2019-11-26 15:17:04 2019-11-27 07:48:32 26  Xiaomi  MI MAX 2    wifi    
53437   2019-11-26 17:49:58 2019-11-26 15:17:04 2019-11-27 07:48:32 25  Xiaomi  MI MAX 2    wifi    
53437   2019-11-26 17:49:58 2019-11-26 15:17:04 2019-11-27 07:48:32 24  Xiaomi  MI MAX 2    wifi    
52535   2019-11-22 18:15:44 2019-11-22 13:33:31 2019-11-22 21:18:04 26  samsung SM-G390F    wifi    
52535   2019-11-22 18:15:44 2019-11-22 13:33:31 2019-11-22 21:18:04 24  samsung SM-G390F    wifi    
52535   2019-11-22 18:15:44 2019-11-22 13:33:31 2019-11-22 21:18:04 25  samsung SM-G390F    wifi    
52396   2019-11-21 22:34:05 2019-11-21 12:20:01 2019-11-22 08:14:38 24  samsung SM-G390F    wifi    

我想要类似的东西:

SELECT DISTINCT 
    # COUNT(accept),<-- How to find this?
    # COUNT(reject),<-- How to find this?
    nms_employee_log.begin,
    nms_employee_log.end,
    nms_employee_log.zone_id,
    nms_employee_log.device_type,
    nms_employee_log.model,
    nms_employee_log.wifi_network
    FROM  nms_employee_log where employee_id= 11

如何获得COUNT(accept)COUNT(reject)

什么是实现此目的的最佳查询?

和最佳表现?

mysql sql count mysql-5.7
1个回答
0
投票
根据最初的问题猜测,应该可以使用CTE(如果您使用的是MySQL 8+)或子选择和CASE SUM,则可以做到这一点>

SELECT SUM(case when accept is not null then 1 else 0 end) as countAccept ,SUM(case when reject is not null then 1 else 0 end) as countReject FROM ( SELECT DISTINCT nms_alert.id as accept, nms_alert_not_attend.nms_alert_id as reject, nms_alert.alert_begin, nms_employee_log.zone_id, nms_employee_log.device_type, nms_employee_log.model, nms_employee_log.wifi_network FROM `nms_alert` INNER JOIN nms_alert_not_attend on nms_alert.id = nms_alert_not_attend.nms_alert_id INNER JOIN nms_employee_log on ( nms_employee_log.begin <= alert_begin AND nms_employee_log.end >= alert_begin ) WHERE nms_alert_not_attend.not_attend_id = 3 AND nms_alert.user_attended_id=3 AND nms_employee_log.employee_id= 11 ) s

© www.soinside.com 2019 - 2024. All rights reserved.