I'm trying to create a query that must returns the values from table Share_users_types
that do not exists in table Shares
according to some conditions.
Share_types table
----------------
ID | Description
1 'Anual'
2 'Monthly'
Share_users_types table
-----------------------------
ID | user_id | share_type_id
1 1 1
2 1 2
3 2 1
Shares table
--------------------------------------------
ID | user_id | share_type_id | Year | Month
1 1 1 2015 null
2 1 2 2015 05
3 1 1 2016 null
4 2 1 2015 null
If I set the following conditions in my query, it should return from the table Share_users_type
:
- Year
2016
, Share typeAnual
-> 1 results ->User_id
: 2 - Year
2015
, Share typeAnual
-> 0 results - Year
2017
, Share typeAnual
-> 2 results ->User_id
: 1 and 2 - Year
2017
, Month 06, Share typeMonthly
-> 1 result ->User_id
: 1
With this said I have tried without any success the following queries:
SELECT sharetype.user_id
FROM share_users_types AS sharetype
LEFT JOIN shares AS share ON share.user_id = sharetype.user_id
WHERE share.user_id IS NULL AND
share.year != '2016' AND share_type_id = 1
SELECT sharetype.user_id
FROM share_users_types AS sharetype
LEFT JOIN shares AS share ON share.user_id = sharetype.user_id
WHERE share.year = '2016' AND share_type_id = 1