I have this kind of email
table
id | url | id1 | id2 | mail_date | message
Example of my rows
id | url | id1 | id2 | mail_date | message
1 | aaa | 2 | 8 | JAN 2016 | blah blah
2 | bbb | 4 | 8 | FEB 2016 | blah blah
3 | aaa | 8 | 2 | MAR 2016 | blah blah
4 | bbb | 8 | 4 | APR 2016 | blah blah
5 | bbb | 4 | 8 | MAY 2016 | blah blah
6 | aaa | 2 | 8 | JUN 2016 | blah blah
7 | bbb | 8 | 4 | JUL 2016 | blah blah
8 | aaa | 8 | 2 | AUG 2016 | blah blah
I have this kind of query
SELECT DISTINCT url, id1, id2 FROM email WHERE id1 = 8 OR id2 = 8 ORDER BY mail_date DESC
My problem is that when I do this whether in phpMyAdmin
or in php code
I get a result like this:
url | id1 | id2
bbb | 8 | 4
aaa | 8 | 2
bbb | 4 | 8
aaa | 2 | 8
What I want is to get results that who has a latest email sent or received by a user, like this:
url | id1 | id2
aaa | 8 | 2
bbb | 8 | 4
aaa | 2 | 8
bbb | 4 | 8
What I want is the DISTINCT
will pick the unique and the latest (based on mail_date
) rows in the email
table, like my want example.
url | id1 | id2
aaa | 8 | 2 //AUG 2016
bbb | 8 | 4 //JUL 2016
aaa | 2 | 8 //JUN 2016
bbb | 4 | 8 //MAY 2016
BUT! DISTINCT
will pick the first unique row of the query and it will get the older mail_date
, like my problem example.
url | id1 | id2
bbb | 8 | 4 //APR 2016
aaa | 8 | 2 //MAR 2016
bbb | 4 | 8 //FEB 2016
aaa | 2 | 8 //JAN 2016
Is there a way DISTINCT
will cooperate with ORDER BY mail_date
the way that I wanted to?
UPDATE
Sorry guys, I made a mistake regarding with url
, I updated it now.