Select rows from specific date with left join

cheeZer

I'm currently working on a simple employee scheduling tool and have a problem with a SQL query. To explain my problem let use this two very abstract tables.

The first table simply consists of the employees

employees
======================
empId | name    | ...
----------------------
10    | Scott   |
11    | Schrute |
12    | Halpert |
13    | Howard  |

In the second table you find the assigned tasks to each employee by day.

tasks
==============================================
tasId | name    | task     | date       | ...
----------------------------------------------
10    | Scott   | Support  | 2014-02-17 |
11    | Scott   | Bugfix   | 2014-02-18 |
12    | Halpert | Bugfix   | 2014-02-17 |
13    | Halpert | Develop  | 2014-02-18 |
14    | Howard  | Support  | 2014-02-17 |

Now I want to know what the employees are working on on Feb 17th or if they have no tasks planned for that day. I use the following SQL query to do that.

SELECT e.name, t.task
FROM employees e LEFT JOIN tasks t ON e.name = t.name
WHERE date IS NULL OR date = DATE('2014-02-17')

The result delivers exactly what I need:

name    | task
--------------------
Scott   | Support
Schrute | NULL
Halpert | Bugfix
Howard  | Support

And now to my problem. If I want to see the tasks of Feb 18th I get this result set:

name    | task
--------------------
Scott   | Bugfix
Schrute | NULL
Halpert | Develop

The reason to this is obvious, the date of Howard's tasks are neither NULL nor do they equal 2014-02-18.. What would be the best way to get the desired result?

I use MySQL and PHP.

(Sorry for the stupid title, I couldn't think of anything better..)

GarethD

Move your filter to the join predicate:

SELECT e.name, t.task
FROM employees e 
LEFT JOIN tasks t ON e.name = t.name AND t.date = DATE('2014-02-18');

Your query as it is will only return people who have no tasks at all, or have tasks on the date specified. It will omit people who have tasks, but not on the date specified. Consider the results of joining your sample data with no where clause:

empId | name    | task     | date       | ...
----------------------------------------|
10    | Scott   | Support  | 2014-02-17 |
10    | Scott   | Bugfix   | 2014-02-18 |
11    | Schrute | NULL     |   NULL     |
12    | Halpert | Bugfix   | 2014-02-17 |
12    | Halpert | Develop  | 2014-02-18 |
13    | Howard  | Support  | 2014-02-17 |

As you can see there is no record for Howard where Date = 2014-02-18, or where Date is null, this is why no record is returned for Howard. When you add the filter to the join predicate your results become:

empId | name    | task     | date       | ...
----------------------------------------|
10    | Scott   | Bugfix   | 2014-02-18 |
11    | Schrute | NULL     |   NULL     |
12    | Halpert | Develop  | 2014-02-18 |
13    | Howard  | NULL     |   NULL     |

Which I think is the desired results.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL query with left join to get rows in decreasing order from a specific date

From Dev

Select rows from table by id of a left join table

From Dev

Select rows with a specific date

From Dev

select from LEFT OUTER JOIN

From Dev

left join same field select all rows

From Dev

MySQL select rows where left join is null

From Dev

Select rows with Left Outer Join and condition - MySQL

From Dev

MySQL LEFT JOIN multiple rows from TableB

From Dev

Get count of rows from LEFT JOIN

From Java

How to left_join() two datasets but only select specific columns from one of the datasets?

From Dev

Left Join without duplicate rows from left table

From Dev

Why LEFT OUTER JOIN reduces rows from left table?

From Dev

LEFT join not returning all rows from LEFT table

From Dev

Select data from two tables with LEFT JOIN

From Dev

Left Join from Select Zend Framework

From Dev

SELECT from different table and LEFT JOIN

From Dev

Hide column from a left join select

From Dev

MySQL - Select last rows of LEFT JOIN column based on the fields

From Dev

Need to understand specific LEFT OUTER JOIN behavior in SQL SELECT

From Dev

Select distinct rows from a table with an inner join

From Dev

Select values from different rows in a mysql join

From Dev

LEFT JOIN with a specific condition

From Dev

Left Join on MAX(DATE)

From Dev

Left Join on MAX(DATE)

From Dev

Oracle, LEFT OUTER JOIN not returning all rows from left table, instead behaving like INNER JOIN

From Dev

Left join without multiple rows from right table

From Dev

Count rows from LEFT JOIN where column is value

From Dev

Left join is not returning all rows from other table

From Dev

Selecting next N rows from table with left join

Related Related

HotTag

Archive