Possible Duplicate:
Retrieving the latest note (by timestamp) in a single query from a 1:n table
Hi not sure what I should be doing here as never had to make a query like this.
What I need to do is join a table called _notes with _leads,
Each client has a unique ref and they have several notes in the _notes table.
What I want to do is produce a query that will show all the fields in _leads + the latest note from each client.
What I have so far is,
SELECT
_leads.ID,
_leads.Title,
_leads.Firstname,
_leads.Lastname
_notes.Note,
_notes.`Date`
FROM
_leads
Left Join _notes ON _leads.ID = _notes.Lead_ID
GROUP BY _leads.ID
This Only shows me the first note of each client,
is there a way of getting the query to do what I want.
A subquery is my usual solution to this type of difficulty
If your _notes table also has an ID field then the below could be useful also and would be slightly cleaner using a subquery to return the highest ID associated with each lead and then joining _notes in its entirety based on that