MySQL: LEFT JOIN not working as expected

The WHERE date(attIn) like '2016-07-02%' is converting the LEFT join to an INNER join. The condition should be moved to the ON clause.

Also:

  • It's not good practise to use LIKE for dates comparison
  • Using functions on columns (like the date()) before comparing it makes indexes useless. It's better to make the condition sargable.

The query corrected:

SELECT e.eno, e.name, e.dept, a.attIn 
FROM employees AS e 
  LEFT JOIN attendanceIn AS a 
    ON  e.eno = a.eno
    AND a.attIn >= '2016-07-02'
    AND a.attIn  < '2016-07-03' ;

Tested at SQLfiddle.

Tags:

Mysql

Join