Mysql组按天

时间:2017-03-24 18:49:51

标签: mysql

这是我的表格:

car_event 
  id
  start_date

employee_car_event 
  car_event_id
  employee_id

有些员工在一天工作两次。

在我的例子中,员工1和2在同一天工作两次。

我想知道每位员工的工作天数

我尝试做:

select 

   count(employee_car_event.id) as nbrJour, 
   employee_car_event.employee_id as idEmployee,    
   DATE_FORMAT(car_event.startDate, '%Y%m%d') as gday

 from employee_car_event 
    left join 
    car_event on car_event.id = employee_car_event.car_event_id 
    group by gday, employee_car_event.employee_id

但我有这个:

2 | 1 | 20170320
2 | 2 | 20170320

我想要:

1 | 1 | 20170320
1 | 2 | 20170320

非常感谢您的帮助

1 个答案:

答案 0 :(得分:2)

尝试使用此查询

select 

   count(distinct employee_car_event.employee_id) as nbrJour, 
   employee_car_event.employee_id as idEmployee,    
   DATE_FORMAT(car_event.startDate, '%Y%m%d') as gday

 from employee_car_event 
    left join 
    car_event on car_event.id = employee_car_event.car_event_id 
    group by gday, employee_car_event.employee_id