views:

12

answers:

1

I have an Employee model and a Shift model. Employees have many shifts and shifts have a date.

I want to be able to select all employees that are available for a given date i.e. select all employees where no associated shifts exists for a given date.

I can select all employees without a shift like this:

SELECT users.* FROM users
LEFT JOIN shifts ON users.id = shifts.user_id
WHERE shifts.user_id IS NULL;

But this is not date specific.

Thanks.

A: 
SELECT users.* 
FROM users 
LEFT JOIN shifts ON users.id = shifts.user_id 
    AND shifts.Date = @SomeDate
WHERE shifts.user_id IS NULL; 
RedFilter
All existing shifts have a date and a user_id, so this returns an empty set.
user88
@user88: try my updated query
RedFilter
Updated one is perfect. Thanks.
user88