473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

how should I write this query

gregerly
192 Expert 100+
I have posted this over at the MySQL forum with no responses, so I thought I might pick the brains of the PHP gurus... I'm having an issue with an SQL query. Heres what I have:

2 tables, client_tbl and appt_table, they join using client_id

I'm trying to write a report that will show me:

a) Any clients that have not had an appointment in X amount of time.
This would be done using DATE_SUB(CURDATE(), INTERVAL $time $units) and comparing that against the appointment date in the appt_table.

b) Any clients that have not ever been scheduled for an appointment.
This would be done by finding clients that have no records in the appointment table.

I'm hoping this can be done in one query, but I'm thinking it's not possible. What I have now works only if a client has previously been scheduled (so part a above works), but if a client has never been scheduled, they are not included in the query results.

Any idea how I can select something if it's NOT in another table?

My current SQL is below:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM client_tbl LEFT JOIN appt_table USING (client_id) WHERE appt_table.appt_date_time < DATE_SUB( CURDATE(), INTERVAL 2 week)
The obvious thing here is that if a client hasn't ever been scheduled there is no appt_date_time in the database so the where fails and the client isn't included in the result. This is where i need help!

Again, I apologize for the double post, but I wasn't getting any takers in the other forum. Mods, please feel free to edit / remove this post if I'm showing bad form.. :)

Greg
Apr 12 '09 #1
2 1130
Markus
6,050 Expert 4TB
SQL isn't my strong point, so please excuse this reply if it's utter bull.

Anyway, I'll explain this as best as I can.

I have tables projects, project_releases and project_changelogs.

Now, there are going to be times when I need to know if a project (taken from the projects table) has any releases, without running SQL queries and counting the results. So, I simply have a flag in the projects table has_release. When a project is initially created (no releases) this is defaulted to 0, and when a release is added I then update this to 1.

Maybe this sort of 'flag' could be of help to you.
Apr 12 '09 #2
gregerly
192 Expert 100+
Right, I see the theory here. I will need to see how I can work that in. Thanks for the idea!
Apr 12 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
0
by: Tanamon | last post by:
Hello All, I am a MySQL newbie trying to write a query that selects file_name records possessing the highest numbered version for that unique file_name. I show sample data and two trial queries...
7
by: Scott | last post by:
I need help to modify the code below to pass url variables from a framset. The click to run this will be in the mainFrame. This script works well in a non-frame page. Grabs the current url...
2
by: laredotornado | last post by:
Hello, Using PHP 4 and MySQL 4, how would I write a function such that it takes a valid MySQL query, and a column on which to order the query, and return a new query ordered by that column? ...
3
by: Jason Huang | last post by:
Hi, In my C# windows form project, I would like to have a function which will write the SQL query result into an XML file. Would someone tell me how to do this? Thanks for help. Jason
10
by: sandraz444 | last post by:
I have an expression in the query under my form to autofill the date under a certain condition but it wont write to the underlying table?? The date shows in the form but not the table. Does anyone...
1
by: lorirobn | last post by:
Hi, I have a query that I have been using as a record source for a form with no problems. I just created a new "addnew" form, and added 20 records to the table with this form. The problem I...
2
by: Himmel | last post by:
Hello! The reference database I currently use runs queries that pull data from hundreds of tables in order to create user-friendly form view. The problem is that these queries can take upwards of...
1
by: manikandaprabhu | last post by:
hi all, i am manikandaprabhu. can you help me out in writing the query which should meet the standards and also must have less exection time.how to write a perfect query.And the procedures...
5
by: Jon Skeet [C# MVP] | last post by:
On Sep 9, 9:41 am, raylopez99 <raylope...@yahoo.comwrote: It's tricky in .NET for two reasons: 1) LINQ doesn't have any concept of "remove" 2) List<T>.RemoveAll doesn't pass in the index...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.