PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 11th, 2004, 04:07 PM
ronmegga ronmegga is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 2 ronmegga User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
MySQL query from PHP with inner join and MAX(date)

I am trying to create a query in MySQL with PHP. I have 2 tables , client_info and activity. The user will input 2 dates creating a range(to and from). I am trying to get the most recent activity.next_appt_date for a client, compare it to the date range and if a client's most recent activity.next_appt_date is with in the range given, then return that client's info as well at the activity.next_appt_date. i cant seem to get the query to work correctly, here is what I have so far...

$appt_sql = "SELECT a.ssn,MAX(a.date_next_appt),c.fname,c.lname,c.ssn FROM activity as a INNER JOIN client_info as c ON a.ssn=c.ssn WHERE (MAX(a.date_next_appt) > '$date_from' AND MAX(a.date_next_appt) < '$date_to') GROUP BY c.fname,c.lname,c.ssn";

the reason I need the most recent date being with in the date range is that there may be several entries for a client and there may be a more recent entry for that client so if they have a activity.next_appt_date with in the range but isnt that client's most recent, I dont want that client returned. This may be simple but I am not very familiar with complex MySql queries. Any help would be greatly appreciated.

Reply With Quote
  #2  
Old October 11th, 2004, 10:45 PM
ronmegga ronmegga is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 2 ronmegga User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

Quote:
Originally Posted by ronmegga
I am trying to create a query in MySQL with PHP. I have 2 tables , client_info and activity. The user will input 2 dates creating a range(to and from). I am trying to get the most recent activity.next_appt_date for a client, compare it to the date range and if a client's most recent activity.next_appt_date is with in the range given, then return that client's info as well at the activity.next_appt_date. i cant seem to get the query to work correctly, here is what I have so far...

$appt_sql = "SELECT a.ssn,MAX(a.date_next_appt),c.fname,c.lname,c.ssn FROM activity as a INNER JOIN client_info as c ON a.ssn=c.ssn WHERE (MAX(a.date_next_appt) > '$date_from' AND MAX(a.date_next_appt) < '$date_to') GROUP BY c.fname,c.lname,c.ssn";

the reason I need the most recent date being with in the date range is that there may be several entries for a client and there may be a more recent entry for that client so if they have a activity.next_appt_date with in the range but isnt that client's most recent, I dont want that client returned. This may be simple but I am not very familiar with complex MySql queries. Any help would be greatly appreciated.

Thanks for all of your "No Help". i figured it out on my own...here is the solution..
$appt_sql = "SELECT a.ssn, MAX(a.date_next_appt) as apptdate, c.fname, c.lname FROM activity AS a INNER JOIN client_info AS c ON a.ssn = c.ssn GROUP BY a.ssn, c.fname, c.lname HAVING MAX(a.date_next_appt) BETWEEN '$date_from' AND '$date_to'";

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > MySQL query from PHP with inner join and MAX(date)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT