mysql保留最后5行并删除旧记录

时间:2012-01-28 21:39:18

标签: mysql

我有mysql表的名称和时间已归档,我想为每个名称选择最后5条记录。我无法加入。有什么帮助吗?

示例数据

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')

从这些数据中我想表现出来。最后五个java,mysql,perl和php记录

编辑:根据你的回答,我提出了sql,但它没有按照我想要的方式工作。

 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 <= 5

结果:

+-------+---------------------+----------+-------------+----------+
| 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      |
+-------+---------------------+----------+-------------+----------+

我期待5行perl

mysql> 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)

2 个答案:

答案 0 :(得分:0)

SELECT * FROM TABLE WHERE NAME IN('java') LIMIT 5 UNION SELECT * FROM TABLE WHERE NAME IN('mysql') LIMIT 5 ...

答案 1 :(得分:0)

看一下我的answer on this post ...它可以对条目进行排名,每个“组”(或者你的名字中的名字)返回1。您所要做的就是将“having”子句更改为&lt; = 5,但查询的上下文应该只是您需要的。