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.
Michael SoleDirector of SupportAsked:
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.

Michael SoleDirector of SupportAuthor Commented:
Note I would want this list ordered by name
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
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

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
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

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
Michael SoleDirector of SupportAuthor 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 :)

Guy Hengel [angelIII / a3]Billing EngineerCommented:
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.