I have a list of tasks assigned to multiple users. I'd like to set up a cron job and send out email notifications once an hour to each user. Here's my query:
SELECT t.*, u.name, u.email
FROM tasks t, users u
WHERE t.date_created > DATE_SUB(NOW(), INTERVAL 1 HOUR) AND t.user_id = u.id
ORDER BY t.date_created ASC
Here's the result (I trimmed a few columns to fit better):
+----+-------+---------------------+---------+--------+------+--------------+
| id | title | date_created | user_id | status | name | email |
+----+-------+---------------------+---------+--------+------+--------------+
| 9 | task1 | 2013-09-01 17:56:10 | 2 | active | John | [email protected] |
| 10 | task2 | 2013-09-01 17:57:20 | 1 | active | Tim | [email protected] |
| 11 | task3 | 2013-09-01 17:58:30 | 2 | active | John | [email protected] |
| 12 | task4 | 2013-09-01 17:59:40 | 1 | active | Tim | [email protected] |
+----+-------+---------------------+---------+--------+------+--------------+
The problem is that I can't figure out how to concatenate the tasks belonging to a certain user_id and then send them out to the assigned email. So for the above example there would be two emails sent out:
- task1 and task 3 (to [email protected])
- task2 and task 4 (to [email protected])
GROUP BY emailorGROUP BY user_id?