Here is my table structure:
users:
id | email
emails:
id | subject | body
user_emails:
user_id | email_id
This is a very simple design. However, I need to select the first (lowest) emails.id for all users.id that are not connected in user_emails.
To illustrate:
users:
id | email
1 | email@domain.com
2 | test@lol.com
3 | user@test.com
emails:
id | subject | body
1 | sub1 | body1
2 | sub2 | body2
user_emails:
user_id | email_id
1 | 1
1 | 2
2 | 1
As the data show:
- User 1 received email 1 and 2, so the choice should exclude this user.
- User 2 received only email 1, so he should select user 2 and email 2.
- User 3 did not receive any emails, so he should select user 3 and email 1.
I will do this in PHP, so any logic should be written in PHP, if it is impossible to do only with mySQL.
Thanks in advance
EDIT: , , , . script, , . , .