I want to be able to select a bunch of rows from a table of e-mails and group them by the from sender. My query looks like this:
SELECT
emailID
,fromEmail
,subject
,timestamp
, MIN(read
) asread
FROMincomingEmails
WHEREtoUserID
= '$userID' GROUP BY LOWER(fromEmail
) ORDER BYtimestamp
DESC
The query almost works as I want it--it selects records grouped by e-mail such that there is only one row per from e-mail address. The problem is that the emailID, subject, and timestamp don't necessarily correspond to the most recent of the e-mails of a particular from e-mail address.
For example, it might return:
From: [email protected] Subject: hello
From: [email protected] Subject: welcome
When the records in the database are:
From: [email protected] Subject: hello
From: [email protected] Subject: programming question
From: [email protected] Subject: welcome
If the programming question e-mail is the most recent, how can I get MySQL to select that record when grouping the e-mails?