Selecting distinct values from mysql with largest timestamp

Posted by user987048 on Stack Overflow See other posts from Stack Overflow or by user987048
Published on 2012-04-15T04:47:34Z Indexed on 2012/04/15 5:30 UTC
Read the original article Hit count: 99

Filed under:
|
|
|

I am building a mail system. The inbox is only supposed to grab the last message (one with the highest time value) of a concatenation of user and sender, where the user or sender is the user ID.

Here is the table structure:

CREATE TABLE IF NOT EXISTS `mail` (
   `user` int(11) NOT NULL,
   `sender` int(11) NOT NULL,
   `body` text NOT NULL,
   `new` enum('0','1') NOT NULL default '1',
   `time` int(11) NOT NULL,
   KEY `user` (`user`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

So, with a table with the following data:

user      sender     new      time
*****************************************
1         0          0        5
1         0          0        6
2         1          0        7
1         0          1        8
1         2          0        9
1         0          1        11
1         2          1        12

I want to select the following: WHERE USER OR SENDER = X (in this case, 1)

user      sender     new      time
*****************************************
2         1          0        7
1         2          0        9
1         0          1        11

How would I go about doing something like this?

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql