开发者

Rails/MYSQL - Find all with no association

开发者 https://www.devze.com 2023-01-20 01:04 出处:网络
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 al开发者_开发知识库l employees that are available for a given date i.e. sele

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 al开发者_开发知识库l 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.


SELECT users.* 
FROM users 
LEFT JOIN shifts ON users.id = shifts.user_id 
    AND shifts.Date = @SomeDate
WHERE shifts.user_id IS NULL; 
0

精彩评论

暂无评论...
验证码 换一张
取 消