您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL 分组后取时间最新记录

2026/1/1 23:30:16发布19次查看
如题,我在网上也找过相关解决方法,很多解答都是这么一句sql语句:
select id,accountid,mark,max(createtime) as latest from accountmark as b group by accountid
使用max函数。但是在我查出来的数据中似乎有些不对,如图,反白的那一条数据,mark字段和createtime字段根本不对应啊!
这是怎么回事?使用max函数后在分组这样靠谱吗?
还有一条语句:select *,count(accountid) as num from 
(select * from accountmark order by createtime desc) `temp`  
group by accountid order by createtime desc 
这样查出来的数据是对的
但是,我需要创建视图,mysql中视图里不允许出现查询子句。求大神些一条sql语句,能实现既不出现子句,又能查出正确数据。谢谢!
解决方法:
select * from accountmark as b where not exists(select 1 from accountmark where accountid= b.accountid
and b.createtime 以上就是mysql 分组后取时间最新记录的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product