求一个单表的SQL语句

learnner 2011-08-13 01:24:26
有表4个字段
id(主键) uid mydatetime amount

现在要找出uid=5的mydatetime最大的那条记录,表的记录比较多,求最高效写法。非高效写法也没有写出来

我的尝试:

select id,uid,max(mydatetime),amount from table1 where uid=5 group by id,uid,amount;

结果是多条记录

请帮助
...全文
97 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2011-08-14
  • 打赏
  • 举报
回复
如果为了提高效率,则需要创建 (uid,mydatetime)的索引。
ACMAIN_CHM 2011-08-14
  • 打赏
  • 举报
回复
select * 
from table1
where uid=5
order by mydatetime desc
limit 1;
徐明勋 2011-08-14
  • 打赏
  • 举报
回复
select a.id,a.uid,a.mydatetime,a.amount from test15 a inner join
(select uid,max(mydatetime) tm from test15 where uid=5) b
on a.uid=b.uid and a.mydatetime=b.tm where a.uid=5
rucypli 2011-08-13
  • 打赏
  • 举报
回复
高效的做法就是uid上必须有索引
nicenight 2011-08-13
  • 打赏
  • 举报
回复
要只是用一次就不用管高不高效了:

select * from table1 where uid=5 order by mydatetime desc limit 1

56,938

社区成员

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

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