I've got 3 tables which store the following values:
Users:
user_id
email
password
status
Scheduled_Jobs:
jobs_id
jobs_userid
date
status
Last_Update:
update_id
update_jobsid
status
When a user is created there is also a list of scheduled jobs created on a monthly bases (so for example 1 job/month based on the date registered). When a user finishes a job the Last_Update table is getting updated.
What I'm currently doing is trying to see how many users are up-to-date and have no pending jobs.
I'm currently going through a loop checking out which users have a status = 1
then their jobs status is also 1 and date is latest (date <= curdate(), so I can grab the latest job) and finally if there is an update in that table for that jobid.
I'm wondering if is it possible to get the same results but with just a single mysql query.
Update (an example):
Users (user_id | email | password | status)
1 | asd@asd.com | xxx | 1
2 | asd2@asd.com | xxx | 1
3 | asd3@asd.com | xxx | 2
4 | asd4@asd.com | xxx | 1
5 | asd3@asd.com | xxx | 1
Scheduled_Jobs (job_id | user_id | date | status)
1 | 1 | 05/09/2019 | 1
2 | 1 | 05/10/2019 | 1
3 | 1 | 05/11/2019 | 1
4 | 1 | 05/12/2019 | 1
5 | 2 | 07/10/2019 | 1
6 | 2 | 07/11/2019 | 1
7 | 2 | 07/12/2019 | 1
8 | 3 | 10/10/2019 | 2
9 | 4 | 13/10/2019 | 1
10 | 4 | 13/11/2019 | 1
11 | 5 | 15/10/2019 | 1
12 | 5 | 15/11/2019 | 1
13 | 5 | 15/12/2019 | 1
Last_Update (update_id | job_id | status)
1 | 1 | 1
2 | 2 | 1
3 | 3 | 1
4 | 5 | 1
5 | 8 | 1
6 | 9 | 1
7 | 11 | 1
Based on the example above we've got 4 active users with scheduled jobs. The result will be (based on today) that 3 out of those 4 users have no pending jobs. As you can see userid 2 have missed a job (jobid = 6).