求查询语句,同文件名时间最大的设置状态

Fallenking 2014-05-22 03:41:32
有一组数据

select archiveid, filetime, filename , status
from file_table
order by filename ASC;


+-----------+---------------------+----------+--------+
| id| filetime| filename | status |
+-----------+---------------------+----------+--------+
| 1 | 2014-05-21 01:24:24 | c1.txt | 1 |
| 8 | 2014-05-23 01:24:24 | c1.txt | 1 |
| 10 | 2014-05-21 04:24:26 | c2.txt | 1 |
| 2 | 2014-05-21 01:24:25 | c3.txt | 1 |
| 9 | 2014-05-21 02:24:25 | c3.txt | 1 |
| 3 | 2014-05-21 01:24:26 | c4.txt | 1 |
| 5 | 2014-05-21 03:24:26 | c4.txt | 1 |
| 6 | 2014-05-21 05:24:26 | c4.txt | 1 |
| 7 | 2014-05-22 01:24:26 | c4.txt | 1 |
+-----------+---------------------+----------+--------+

数据库file_table表有4组文件,c1 ~ c4.txt,录入的时间filetime列不一样,每组文件将最大时间的status位设置成3。 求查询语句。

期待结果

+-----------+---------------------+----------+--------+
| id| filetime| filename | status |
+-----------+---------------------+----------+--------+
| 1 | 2014-05-21 01:24:24 | c1.txt | 1 |
| 8 | 2014-05-23 01:24:24 | c1.txt | 3 |
| 10 | 2014-05-21 04:24:26 | c2.txt | 3 |
| 2 | 2014-05-21 01:24:25 | c3.txt | 1 |
| 9 | 2014-05-21 02:24:25 | c3.txt | 3 |
| 3 | 2014-05-21 01:24:26 | c4.txt | 1 |
| 5 | 2014-05-21 03:24:26 | c4.txt | 1 |
| 6 | 2014-05-21 05:24:26 | c4.txt | 1 |
| 7 | 2014-05-22 01:24:26 | c4.txt | 3 |
+-----------+---------------------+----------+--------+
...全文
63 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2014-05-22
  • 打赏
  • 举报
回复
参考下贴中的多种方法 http://blog.csdn.net/acmain_chm/article/details/4126306 [征集]分组取最大N条记录方法征集,及散分....
benluobo 2014-05-22
  • 打赏
  • 举报
回复
select A.id, A.filetime, A.filename, (select if(count(*)=0,3,status) from file_table where filename = A.fileName and filetime > A.filetime) status from file_table A

56,681

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧