Background, I have a cron job that queries keywords associated with posts, if that keyword is found, a user is emailed about the post.
The cron job runs every minute so once a user has been emailed that notification should not happen again. The following query runs:
SELECT DISTINCT p.id AS post_id, u.display_name, u.id AS user_id, u.email, k.keyword, k.id AS keyword_id
FROM posts p, user_subscriptions us, keywords_users ku, keywords k, users u
LEFT JOIN keyword_subscription_sent kss ON kss.user_id = u.id
LEFT JOIN keywords_posts kp ON kss.post_id = kp.post_id
WHERE us.keywords = 1
AND ku.user_id = u.id
AND ku.keyword_id = k.id
AND kp.keyword_id = k.id
#AND kss.user_id IS NULL AND kss.post_id IS NULL
LIMIT 0, 200
The query works as expected when the table keywords_posts kp is populated.
If the table keywords_posts kp is empty the query will never return anything. I attempted to fix this by adding to the WHERE clause:
AND kss.user_id IS NULL AND kss.post_id IS NULL
But adding this to the WHERE clause will cause the query to never return anything at all at any time.
The desired result is:
The query will return all rows except listed in the keywords_posts kp table.