473,408 Members | 1,683 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,408 software developers and data experts.

problem with select query

gregerly
192 Expert 100+
Hello, I'm not sure how to properly format a query that I need to select some database information. I have two tables, one named "appts" and another "clients". They are linked by client_id. I'm trying to write a query that will show me clients that haven't had an appointment is "X" amount of "time units". I'm doing this like:

SELECT * FROM appts JOIN clients ON appts.client_id = clients.client_id WHERE DATE(appts.appt_date) < DATE_SUB( CURDATE(), INTERVAL 1 MONTH) GROUP BY appts.client_id;

This works great if they users have already had an appointment. The problem is that users that haven't had an appointment are not being returned by the query (which I get). My question is there any way to also return those clients that haven't had an appointment yet (return those clients that have not already been booked by my system).

I realize I could just select those also in a separate query, but it would be nice to have them as part this data set? Any ideas?

Thanks!

Greg
Apr 10 '09 #1
4 2360
code green
1,726 Expert 1GB
This works great if they users have already had an appointment. The problem is that users that haven't had an appointment are not being returned by the query.
The way around this type of problem is a LEFT JOIN.
And I would JOIN the tables the other way round in your case.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM clients LEFT JOIN appts USING(client_id) 
  2. WHERE DATE(appts.appt_date) < DATE_SUB( CURDATE(), INTERVAL 1 MONTH)
  3. GROUP BY appts.client_id;
If your JOIN has to be the other way then your WHERE clause from appts table will have to accept NULLs
Expand|Select|Wrap|Line Numbers
  1. OR DATE(appts.appt_date) IS NULL
I have no idea what VoraAnkit is trying to tell you.
Apr 14 '09 #2
gregerly
192 Expert 100+
Your the man Code Green, this is just what I was looking for. I was making it more complicated than it had to be. Thanks for your answer!

Greg
Apr 15 '09 #3
gregerly
192 Expert 100+
Ok, I'm back and this is getting a little old. The suggestion that Code Green gave was spot on, but I realized it was still not quite what I need. The code shouldn't be a problem, rather it seems the logic I'm having a problem with.

I need to create a report that lets the user know which of their clients have been in for an appointment in X amount of INTERVAL. The query I have now basically says, "Select all clients where the appointment date is less than the specified time interval". What I really need is "Select all client where the MAX(appointment date) is less than the specified time interval.

The problem is when I use MAX() inside the where, I get a mysql error. How would I write the query to only look at the max appointment date? My code is below:

Currently the sql looks like this:

Expand|Select|Wrap|Line Numbers
  1. "SELECT * FROM clients_tbl JOIN appt_table USING(client_id) WHERE appt_table.appointment_date < DATE_SUB ( CURDATE(), INTERVAL 1 WEEK );" 
Ideally what I need is:

Expand|Select|Wrap|Line Numbers
  1. "SELECT * FROM clients_tbl JOIN appt_table USING(client_id) WHERE MAX(appt_table.appointment_date) < DATE_SUB ( CURDATE(), INTERVAL 1 WEEK );" 
If anyone has any ideas, I'm all ears.

Thanks again guys!

Greg
Apr 17 '09 #4
gregerly
192 Expert 100+
SOLVED: because I gave up. I don't think what I want to do can be done with MySQL (not because of mysql, but because of my DB set up). I've decided to just do the date comparisons with PHP rather than mysql. It's less efficient, but I don't see a way to do it with straight mysql.

Thanks for your help guys!

Greg
Apr 17 '09 #5

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

Similar topics

5
by: Juho Saarikko | last post by:
I made a Python script which takes Usenet message bodies from a database, decodes uuencoded contents and inserts them as Large Object into a PostGreSQL database. However, it appears that the to...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
1
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
3
by: StBond | last post by:
Hi everyone, I am new to Access and Visual Basic so things my be getting across a bit cloudy. I only started using VB for one week. I am having a little problem with the database that I am...
20
by: Development - multi.art.studio | last post by:
Hello everyone, i just upgraded my old postgres-database from version 7.1 to 7.4.2. i dumped out my 7.1 database (with pg_dump from 7.1) as an sql-file with copy-commands and to one file using...
0
by: mjsterz | last post by:
I've been working with VB .NET for less than a year and this is the first time I've posted on one of these groups, so let me apologize beforehand if I'm being unclear, not posting my issue...
9
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just...
6
by: jej1216 | last post by:
I am trying to put together a PHP search page in which the user can select none, one, two, or three fields to search, and then the results php will build the SQL with dynamic where caluses to reflect...
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
10
by: amitabh.mehra | last post by:
Hi I havent used MQT before. Read the online tips and tutorials but none seems to give any hint for my problem. I have a base table (base_table) as: st varchar(25) default...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.