Note that there are some explanatory texts on larger screens.

plurals
  1. POmysql keep last 5 row and remove older records
    primarykey
    data
    text
    <p>I have mysql table with name, and time as filed, I want to select last 5 records for each name. I am not able do this join. any help?</p> <p>sample data</p> <pre><code>CREATE TABLE `ntest` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) DEFAULT NULL, `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) INSERT INTO `ntest` VALUES (1,'java','2012-01-28 21:14:01'), (2,'java','2012-01-28 21:14:03'), (3,'java','2012-01-28 21:14:04'), (4,'perl','2012-01-28 21:14:08'), (5,'perl','2012-01-28 21:14:09'), (6,'perl','2012-01-28 21:14:09'), (7,'perl','2012-01-28 21:14:10'), (8,'perl','2012-01-28 21:14:11'), (9,'perl','2012-01-28 21:14:11'), (10,'perl','2012-01-28 21:14:12'), (11,'perl','2012-01-28 21:14:13'), (12,'perl','2012-01-28 21:14:14'), (13,'mysql','2012-01-28 21:14:21'), (14,'mysql','2012-01-28 21:14:22'), (15,'mysql','2012-01-28 21:14:22'), (16,'mysql','2012-01-28 21:14:23'), (17,'mysql','2012-01-28 21:14:23'), (18,'mysql','2012-01-28 21:14:24'), (19,'mysql','2012-01-28 21:14:25'), (20,'mysql','2012-01-28 21:14:25'), (21,'mysql','2012-01-28 21:14:26'), (22,'mysql','2012-01-28 21:14:26'), (23,'mysql','2012-01-28 21:14:27'), (24,'mysql','2012-01-28 21:14:27'), (25,'php','2012-01-28 21:21:27') </code></pre> <p>from this data I want to displaly. last five java,mysql, perl and php records</p> <p>Edit: based on your answer I came up with sql, but its not working the way I want.</p> <pre><code> select NameCounts.name, NameCounts.updated_at, NameCounts.numnames, @RankSeq := if( @LastDistrict = NameCounts.name, @RankSeq +1, 1 ) DistRankSeq, @LastDistrict := NameCounts.name as ignoreIt from ( select a.name, a.updated_at, count(*) as numnames from ntest a group by a.name, a.updated_at order by a.name, a.updated_at desc ) NameCounts JOIN (select @RankSeq := 0, @LastDistrict = 0 ) SQLVars HAVING DistRankSeq &lt;= 5 </code></pre> <p>Result:</p> <pre><code>+-------+---------------------+----------+-------------+----------+ | name | updated_at | numnames | DistRankSeq | ignoreIt | +-------+---------------------+----------+-------------+----------+ | java | 2012-01-28 19:21:47 | 1 | 1 | java | | java | 2012-01-28 16:14:04 | 1 | 3 | java | | java | 2012-01-28 16:14:03 | 1 | 5 | java | | mysql | 2012-01-28 16:14:27 | 2 | 1 | mysql | | mysql | 2012-01-28 16:14:26 | 2 | 3 | mysql | | mysql | 2012-01-28 16:14:25 | 2 | 5 | mysql | | perl | 2012-01-28 19:22:18 | 1 | 1 | perl | | perl | 2012-01-28 16:14:14 | 1 | 3 | perl | | perl | 2012-01-28 16:14:13 | 1 | 5 | perl | | php | 2012-01-28 19:21:40 | 1 | 1 | php | | php | 2012-01-28 16:21:27 | 1 | 3 | php | +-------+---------------------+----------+-------------+----------+ </code></pre> <p>I expect 5 rows of perl </p> <pre><code>mysql&gt; select * from ntest where name='perl'; +----+------+---------------------+ | id | name | updated_at | +----+------+---------------------+ | 4 | perl | 2012-01-28 16:14:08 | | 6 | perl | 2012-01-28 16:14:09 | | 7 | perl | 2012-01-28 16:14:10 | | 9 | perl | 2012-01-28 16:14:11 | | 10 | perl | 2012-01-28 16:14:12 | | 11 | perl | 2012-01-28 16:14:13 | | 12 | perl | 2012-01-28 16:14:14 | | 28 | perl | 2012-01-28 19:22:18 | +----+------+---------------------+ 8 rows in set (0.00 sec) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload