【SQL语句】怎么根据需求除去某列重复结果
问题描述如下:
现在的语句是
SQL code
select recvperson,sendTime,sendperson from ofHistroy where sendperson='admin' order by sendTime desc;
得到的结果是
+----------------------+---------------------+------------+
| recvperson | sendTime | sendperson |
+----------------------+---------------------+------------+
| 13012345678@ooooooo | 2012-08-28 12:03:31 | admin |
| 13112345678@ooooooo | 2012-08-28 12:01:31 | admin |
| 13012345678@ooooooo| 2012-08-28 11:56:41 | admin |
| 13112345678@ooooooo | 2012-08-28 10:47:09 | admin |
| 13212345678@ooooooo | 2012-08-28 10:40:58 | admin |
| admin@ooooooo | 2012-08-28 10:33:17 | admin |
| 13212345678@ooooooo | 2012-08-28 10:17:31 | admin |
| 13212345678@ooooooo | 2012-08-27 17:17:44 | admin |
| 13212345678@ooooooo | 2012-08-27 17:14:59 | admin |
| 13212345678@ooooooo | 2012-08-27 17:14:58 | admin |
| 13212345678@ooooooo | 2012-08-27 17:14:57 | admin |
| 13212345678@ooooooo | 2012-08-27 17:14:55 | admin |
+----------------------+---------------------+------------+
12 rows in set
预期结果是:
+----------------------+---------------------+------------+
| recvperson | sendTime | sendperson |
+----------------------+---------------------+------------+
| 13012345678@ooooooo | 2012-08-28 12:03:31 | admin |
| 13112345678@ooooooo | 2012-08-28 12:01:31 | admin |
| 13212345678@ooooooo| 2012-08-28 10:40:58 | admin |
| admin@ooooooo | 2012-08-28 10:33:17 | admin |
+----------------------+---------------------+------------+
需求说明:
1.查询到的recvperson无重复;
2.按照sendTime降序排列;
3.sendperson限制为admin。
------解决方案--------------------
试试这个:
SQL code
select recvperson, max(sendTime) as sendTime,sendperson from ofHistroy where sendperson='admin' group by recvperson order by sendTime desc;