能否用一条SQL语句实现

fangpeng2003 2004-03-28 11:00:03
表里有三个字段

id username content
1 tom aaa
2 jack bba
3 tom bbb
4 jack ccc
5 rose ddd
6 rose rara

我要得到这样的结果
id username content
3 tom bbb
4 jack ccc
6 rose rara

就是说要取到每个人最新的记录即可。
...全文
56 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
playyuer 2004-03-28
  • 打赏
  • 举报
回复
select *
from 表 a
where id = (select max(id) from 表 where username = a.username )


select *
from 表 a
where not exists (select 1 from 表 where username = a.username and id > a.id)
leeboyan 2004-03-28
  • 打赏
  • 举报
回复
select * from table1 where id in(select max(id)as id from table1 group by username)
victorycyz 2004-03-28
  • 打赏
  • 举报
回复

select *
from tablename
where id in ( select max(id)
from tablename
group by username
)
dlpseeyou 2004-03-28
  • 打赏
  • 举报
回复
select username,max(id) as id,max(content) as content
from 表 group by username
progress99 2004-03-28
  • 打赏
  • 举报
回复
select username,max(id) as id,max(content) as content
from 你的表
group by username

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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