【问题标题】:Get Total Count from Current Status Query MySQL从当前状态查询 MySQL 中获取总计数
【发布时间】:2019-02-13 07:27:23
【问题描述】:

我在计算 mysql 查询当前状态的总数据时遇到问题。

简单地说,我有 3 个相互关联的表。

m_shift_schedule     site_shift_schedule      m_ticket
================     ===================     ============
* shift_id   ======>  * shift_id             * ticket_id
* start_time          * shift_date           * ticket_status
* end_time            * user_id   ========>  * ticket_served_by

我的查询:

SELECT user_id,
case when count(m_ticket.ticket_served_by)<2 then 'idle' 
     when count(m_ticket.ticket_served_by)=2 then 'Busy' 
     when count(m_ticket.ticket_served_by)>2 then 'Overload' end as status
FROM site_shift_schedule
LEFT JOIN m_shift_schedule ON site_shift_schedule.shift_id = m_shift_schedule.shift_id
LEFT JOIN m_ticket ON site_shift_schedule.user_id=m_ticket.ticket_served_by
WHERE site_shift_schedule.shift_date =  '2019-02-11' and m_ticket.ticket_status in (4,5,6)
AND CURRENT_TIME > start_time and CURRENT_TIME < end_time
group by user_id

我上面查询的输出:

user_id                           Status
=============================   ============
ismail.rahman.saanin@random.co     Idle
lutfi.aldi.nugroho@random.co       Busy

还有一个问题,如果我想显示输出变成这样,我应该怎么做:

 Status              Total
==================================
 Idle                 1
 Busy                 1

【问题讨论】:

    标签: mysql sql database database-design database-connection


    【解决方案1】:

    您可以在下面尝试 - 使用子查询

    select status,count(user_id) as total
    from
    (
    SELECT user_id,
    case when count(m_ticket.ticket_served_by)<2 then 'idle' 
         when count(m_ticket.ticket_served_by)=2 then 'Busy' 
         when count(m_ticket.ticket_served_by)>2 then 'Overload' end as status
    FROM site_shift_schedule
    LEFT JOIN m_shift_schedule ON site_shift_schedule.shift_id = m_shift_schedule.shift_id
    LEFT JOIN m_ticket ON site_shift_schedule.user_id=m_ticket.ticket_served_by
    WHERE site_shift_schedule.shift_date =  '2019-02-11' and m_ticket.ticket_status in (4,5,6)
    AND CURRENT_TIME > start_time and CURRENT_TIME < end_time
    group by user_id
    )A group by status
    

    【讨论】:

      【解决方案2】:

      group by 中的用例

      SELECT 
      case when count(m_ticket.ticket_served_by)<2 then 'idle' 
           when count(m_ticket.ticket_served_by)=2 then 'Busy' 
           when count(m_ticket.ticket_served_by)>2 then 'Overload' end as status,
           count(*) as total
      FROM site_shift_schedule
      LEFT JOIN m_shift_schedule ON site_shift_schedule.shift_id = m_shift_schedule.shift_id
      LEFT JOIN m_ticket ON site_shift_schedule.user_id=m_ticket.ticket_served_by
      WHERE site_shift_schedule.shift_date =  '2019-02-11' and m_ticket.ticket_status in (4,5,6)
      AND CURRENT_TIME > start_time and CURRENT_TIME < end_time
      group by  case when count(m_ticket.ticket_served_by)<2 then 'idle' 
           when count(m_ticket.ticket_served_by)=2 then 'Busy' 
           when count(m_ticket.ticket_served_by)>2 then 'Overload' end
      

      【讨论】:

      • 请您分享小提琴,因为我认为它不会起作用,因为 group by 子句中缺少 user_id
      • @aldi 你检查了吗?
      猜你喜欢
      • 2019-07-09
      • 2019-07-04
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      相关资源
      最近更新 更多