sql max 问题

xiyaoponewnew 2016-04-14 09:43:08
mysql 表usercount数据
userID count publicDate
49 101 2014-5-6
50 103 2014-5-9
50 108 2014-6-3

CREATE OR REPLACE VIEW usercount_view AS
SELECT userID,count,MAX(publicDate) FROM usercount GROUP BY userID;


本想要的是这个结果
49 101 2014-5-6
50 108 2014-6-3
但却是这个结果
49 101 2014-5-6
50 103 2014-6-3
请高手帮忙修改一下SQL语句,叩谢!!!
...全文
99 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2016-04-14
  • 打赏
  • 举报
回复
参考下贴中的多种方法 http://blog.csdn.net/acmain_chm/article/details/4126306 [征集]分组取最大N条记录方法征集,及散分....
benluobo 2016-04-14
  • 打赏
  • 举报
回复
select a.* from usercount a where not exists (select 1 from usercount b where a.userId=b.userId and a.publicDate < b.publicDate)
rick-he 2016-04-14
  • 打赏
  • 举报
回复
引用 1 楼 u011575570 的回复:
CREATE OR REPLACE VIEW usercount_view AS select userID,count,publicDate from usercount u,(SELECT userID,MAX(publicDate) max_publicDate FROM usercount GROUP BY userID) g where u.userID = g.userID and u.publicDate =g.max_publicDate
group by取的是第一个数据,所以不会去判断数据的一致性。
rick-he 2016-04-14
  • 打赏
  • 举报
回复
CREATE OR REPLACE VIEW usercount_view AS select userID,count,publicDate from usercount u,(SELECT userID,MAX(publicDate) max_publicDate FROM usercount GROUP BY userID) g where u.userID = g.userID and u.publicDate =g.max_publicDate

56,687

社区成员

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

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