Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Consulta agrupada para localizar apenas um registro (employee_id =1 ou nulo)


Experimente isto:
SELECT 
  b.calendar_id,
  a.employee_id,
  a.weekday 
FROM
  (SELECT 
    `weekday`,
    MAX(employee_id) AS employee_id 
  FROM
    calendar 
  WHERE employee_id = 1 
    OR employee_id IS NULL 
  GROUP BY `weekday`) a 
  LEFT JOIN calendar b 
    ON a.weekday = b.weekday 
    AND a.employee_id = b.employee_id ;

Avise-me em caso de esclarecimentos.