Limit a join to one newest row

I have a table of patients and a table of next activity dates. Each time a next activity is scheduled it adds a row with the date and the patient ID. I want to retrieve a list of patients and their newest next activity date.

So example table structures would be
Patients:
id, name
1, John Doe
2, Jane Smith
Next Activity
id, pid, nextActivity
1,1,11/12/2012
2,1,3/15/2011
3,2,11/1/2011
4,2,4/1/2011

Example output:

ID   Name    Next Activity
1    John Doe  11/12/2012
2    Jane Smith  11/1/2011

This is in mysql 5 access via PHP, but I don't really need help with the code just the select statement. I should really know this but I am blanking at the moment.
skioneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

skioneAuthor Commented:
Note I would want this list ordered by name
0
ralmadaCommented:
try

select a.id, a.name, b.mnextactivity
from Patients a
inner join (select pid, max(nextActivity) mnextactivity from NextActivitytable group by pid) b on a.id = b.pid
order by a.name
0
ralmadaCommented:
or simply

select a.id, a.name, max(b.nextactivity) as newestactivity
from Patients a
inner join NextActivitytable b on a.id = b.pid
group by a.id, a.name
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ralmadaCommented:
oops, forgot the name sorting there:

select a.id, a.name, max(b.nextactivity) as newestactivity
from Patients a
inner join NextActivitytable b on a.id = b.pid
group by a.id, a.name
order by a.name
0
skioneAuthor Commented:
Yeah I would of figured that part out. I hadn't even thought of using max, great solution!

I was thinking of using group by and limit in some way but was coming up empty.

Thank you :)

0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.