Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8260517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:07:05+00:00 2026-06-08T03:07:05+00:00

So I am trying to run a query that will select, in this case,

  • 0

So I am trying to run a query that will select, in this case, clients that haven’t had an appointment is X amount of time (2 weeks for instance). It boils down to “show me a list of clients that haven’t had an appointment in 2 weeks”. I’m attempting 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 involved, appt_tbl, and clients_tbl. appt_tbl stores a client_id for each appointment record.

So essentially what I need is to select the “max” appointment for each client, and if it’s < my $target date, include them in the query results. I’ve tried various flavors of queries, queries with sub queries, but I’m stumbling on getting this query right.

My current attempt looks something like:

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 that have never been scheduled (IE won’t appear in the appt_tbl), but not clients who have an appointment booked in the next two weeks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:07:06+00:00Added an answer on June 8, 2026 at 3:07 am
    SELECT a.*
    FROM clients_tbl a
    LEFT JOIN appt_tbl b ON 
              a.client_id = b.client_id AND 
              b.appt_date_time >= CURDATE() - INTERVAL 2 WEEK 
    WHERE b.client_id IS NULL
    

    What this query does first (before the WHERE filtering) is select all clients whether or not they have a scheduled appointment greater than two weeks ago.

    If the client does not have an appointment greater than two weeks ago, the values in the joined table will be NULL. We want all rows where the join conditions did not satisfy (i.e. values in joined table are null), which is done with WHERE b.client_id IS NULL.

    This also includes into the result-set clients who do not have any corresponding appointments at all.

    Clients who have appointments in the future are excluded.

    There is also no need to construct a datetime string in PHP. You can simply do it straight in the query (although you may pass in the number of weeks ago as a parameter).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run a select query that will pull all meds scheduled
I am trying to run a SQL query (SQL Server) that will look up
I'm trying to run this query: SELECT Destaque.destaque, Noticia.id, Noticia.antetitulo, Noticia.titulo, Noticia.lead, Noticia.legenda, Noticia.publicacao,
hi i am trying to make a query that will run records from a
Im trying to write a query that will tell me how much time a
Hi I am trying to run this query in SQL but it is not
Hey guys I am getting this exception when trying to run the query Column
I am trying to run the following query: SELECT COUNT(*) FROM ( SELECT *
I'm currently trying to run a LINQ query over a MS SQL database. This
I want to run a linq query that will return values to my custom

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.