|
|
Question : MySQL Left Join Query with only the most recent value from second table
|
|
I have two tables:
Table 1: name: members fields: memberID, firstname, lastname
Table 2: name: meetings fields: meetingID, meetingdate, greetID
I want to develop a mySQL query in a PHP script that will result in ALL the records in Table 1(members) and only one row from Table 2 (meetings) where greetID = memberID. There can be many rows in Table 2 that could result; I only want the one with the MOST RECENT meetingdate.
Some rows in Table 1 may not have any matching rows in Table 2. I want to include these as well
I've gotten as far as building a LEFT JOIN:
SELECT members.firstname, members.lastname, meetings.meetingdate FROM members LEFT JOIN meetings on memberID = greetID ORDER BY meetingdate
However,I end up with all the matching records from the second table (meetings). I only want the most recent date from the meetings table
Fields I want in this query: members.firstname, members.lastname, members.memberID, meetings.meetingdate
Can anyone help me?
|
Answer : MySQL Left Join Query with only the most recent value from second table
|
|
Oops, shuld have used max instead of min.
SELECT members.firstname, members.lastname, max(meetings.meetingdate) FROM members LEFT JOIN meetings on memberID = greetID group by memberid ORDER BY meetingdate
|
|
|
|
|