I have two tables,
---Jobs---
id,name,.....
---Locations---
id,jobId,Location,date
now a job can have more then one location I want to join the job with it's latest location where jobs.id=location.jobId
and date
is greatest.
I have two tables,
---Jobs---
id,name,.....
---Locations---
id,jobId,Location,date
now a job can have more then one location I want to join the job with it's latest location where jobs.id=location.jobId
and date
is greatest.
Try this:
select * from jobs inner join locations on jobs.id=locations.jobid order by date desc limit 1;
Could be:
SELECT *
FROM Jobs J, Locations L
WHERE J.id=L.jobId
AND L.date = (
SELECT MAX(date)
FROM Locations L2
WHERE L2.jobId=L.jobId
GROUP BY L2.jobId)
Explanation:
In the nested Query I am selecting the maximum date for each Job.
In the external Query i am joining the two tables and fetching only the rows where the date is equal to the MAX(date) i found on the nested query and the Job is the same. This is accomplished by the condition L2.jobId=L.jobId