I have a table with timestamped rows: say, some feed with authors:
CREATE TEMPORARY TABLE `feed` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`author` VARCHAR(255) NOT NULL,
`tm` DATETIME NOT NULL
);
I’d like to sort by tm DESC but in such a way that rows from one author do stick together.
For instance, having
INSERT INTO `feed` VALUES
( 5, 'peter', NOW()+1 ),
( 4, 'helen', NOW()-1 ),
( 3, 'helen', NOW()-2 ),
( 2, 'peter', NOW()-10 ),
( 1, 'peter', NOW()-11 );
The result set should be sorted by tm DESC, but all peter posts go first because his post is the most recent one. The next set of rows should originate from the author with the 2nd most recent post. And so on.
5 peter
2 peter
1 peter
3 helen
2 helen
First we sort authors by recent post, descending. Then, having this “rating”, we sort the feed with authors sorted by recent post.
Create in line view calculating the Min Tm and then join to it.
DEMO