私はmysqlテーブルに名前があり、フィールドにはフィールドがあり、それぞれの名前の最後の5レコードを選択します。私はこの参加をすることができません。どんな助け?私はdisplalyしたい、このデータから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')
。最後の5つのjava、mysql、perl、phpのレコード
編集:あなたの答えに基づいて、私は思いついたが、私が望むように働いていない。
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 |
+-------+---------------------+----------+-------------+----------+
私はperlのの5行
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)
私の名前は動的ですが、100の異なる名前を持つ可能性があります – sfgroups
トップ5をmysqlで利用できません – sfgroups
コードからSQLを生成するだけです。 –