So, I'm trying to run a query that will select, in this case, clients who did not have an appointment, this is X amount of time (for example, 2 weeks). It comes down to "showing me a list of clients who did not have a meeting in two weeks." I am trying to solve this in php by doing something like:
$date = new DateTime; $ago = new DateInterval('P2W'); $target = $date->sub($ago); //query to select clients that aren't scheduled after the $target date $clients = ...;
Two tables are included: appt_tbl and clients_tbl . appt_tbl stores client_id for each record of the record.
So, essentially, I need to select the βmaximumβ destination for each client, and if it is <my $ target date, include them in the query results. I tried various query options, queries with subqueries, but I came across the correctness of the query.
My current attempt looks something like this:
SELECT * FROM clients_tbl INNER JOIN ( SELECT client_id FROM appt_tbl WHERE MAX(appt_date_time) < '2012-07-22' GROUP BY client_id ) appts ON appts.client_id = clients_tbl.client_id;
This should also include clients who have never been scheduled (IE will not appear in appt_tbl), but not clients who have an appointment over the next two weeks.
source share