With my limited knowledge of complex mysql queries I'm trying to retrieve some information from a database.
The story is this; users get an invite to come to our company. On the basis of this one invite, users can get multiple notifications and multiple appointments. I've got three relevant tables:
invites
-------
| id | name    | created    |
-----------------------------
|  1 | someth1 | 2018-02-03 |
|  2 | someth2 | 2018-02-03 |
|  3 | someth3 | 2018-02-03 |
notifications
-------------
| id | inv_id  | message |
--------------------------
|  1 | 101     | hello   |
|  2 | 287     | hi      |
|  3 | 827     | hey     |
appointments
------------
| id | inv_id  | start_at   |
-----------------------------
|  1 | 101     | 2018-02-03 |
|  2 | 287     | 2018-02-08 |
|  3 | 827     | 2018-02-15 |
I currently have a query, which shows a list of notifications send to users, for all invites done after 1 feb 2018, and which have an appointment no later than '2018-03-10'.
SELECT id, inv_id, message
FROM notifications 
WHERE inv_id IN (
    SELECT id
    FROM invites as invite
    WHERE created > '2018-02-01'
    AND id IN (
        SELECT inv_id 
        FROM appointments
        WHERE invite.id = inv_id
        AND start_at < '2018-03-10'
    )
)
ORDER BY inv_id ASC;
The result looks something like this:
| id | inv_id  | message |
--------------------------
|  1 | 101     | hello   |
|  2 | 287     | hi      |
|  3 | 827     | hey     |
I now want to add the start_at of the first appointment for these notifications
| id | inv_id  | message | start_at   |
---------------------------------------
|  1 | 101     | hello   | 2018-02-03 |
|  2 | 287     | hi      | 2018-02-08 |
|  3 | 827     | hey     | 2018-02-15 |
But from here I'm kinda lost in how I should do that.
Does anybody know how I can add the start_at of the first appointment which corresponds to the invite for the respective notification? So it should show the start_at of the first appointment for the invite of the notification inv_id?
 
     
     
    