Home  >  Article  >  Database  >  mysql-一条貌似简单的Mysql问题

mysql-一条貌似简单的Mysql问题

WBOY
WBOYOriginal
2016-06-06 09:43:12986browse

mysql数据表

如何删除部份记录?

设有一份数据表:
userid score year
1 90 1990
2 80 2000
3 70 2010
1 85 2000
2 90 2010
1 95 2010

要求: 对于每个用户来说,仅保留其两条最近的记录,其余的删除,请问如何用一条sql语句搞定?

补充说明:
1. userid是用户的id
2. year是表示该记录的年份,数值越小表明该记录越旧.
3. 保留的最新记录数可能是2, 也可能是3或者更多,因此请勿用"保留一条记录"的做法。

谢谢

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn