These are the three tables I have:
emails
+----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+----------------+ | emailID | int(11) | NO | PRI | NULL | auto_increment | | subject | varchar(100) | YES | | NULL | | | body | text | YES | | NULL | | | userID | int(11) | NO | | NULL | | | date | timestamp | YES | | NULL | | +----------+--------------+------+-----+---------+----------------+
people
+----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | userID | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(50) | YES | | NULL | | | email | varchar(50) | NO | | NULL | | | relation | varchar(50) | YES | | NULL | | +----------+-------------+------+-----+---------+----------------+
toLine
+---------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+---------+------+-----+---------+-------+ | emailID | int(11) | NO | PRI | NULL | | | userID | int(11) | NO | PRI | NULL | | +---------+---------+------+-----+---------+-------+
The field emails.userID is the ID of the sender, so who the email is from.
What I need is a query that will get me emails sent by me (relation='researcher') to my course director (relation='course director'). I don't know how to do the joins so I can use the relation field in two different ways.
I've tried the query:
SELECT count(emails.emailID)
FROM emails
LEFT JOIN people ON emails.userID = people.userID
WHERE people.relation='researcher';
This gets me all the emails sent by me, but I'm having trouble figuring out how to filter this down further based on who the email is sent to.