Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Get all combinations of users that didn't have a period linked

I have this tables :

Declaration :

id     declarant_id
1        2
2        4
3        5

Declarant

id       username
 1        sdfd
 2        sdfsd 
 3        sdfsdf
 4        sdfsd
 5        sdfsdfds

DeclarationPeriods

id        start
1         2018-02-03
2         2018-03-05
3         2018-04-19
4         2018-06-21
5         2018-03-94

DeclarationDeclarationPeriods:

id_declaration         id_declaration_period
 1                      2
 2                      2

Now the idea is to get all users that didn't have a declaration for specific declaration_period: I tried like this :

SELECT u.id user_id, d.id as declaration_id, dp.id as declaration_period_id
FROM user u
RIGHT JOIN declaration d ON u.id = d.declarant_id
INNER JOIN declaration_declaration_periods ddp ON d.id = ddp.declaration_id
RIGHT JOIN declaration_periods dp ON ddp.declaration_periods_id = dp.id
WHERE dp.id >= 357 AND dp.id <= 375

For example I get first user and I check for each period if the user didn't have a declaration for the period I need to get in this query. For example if I have 10 declaration_periods and I get first user, If this user have declarations only for first 4 declaration_periods, I need to get in query last 6 declarations_periods with their id. Please help me.

Comments