Mysql应用获取MySQL的表中每个userid最后一条记录的方法

Mysql应用获取MySQL的表中每个userid最后一条记录的方法,第1张

概述介绍《Mysql应用获取MySQL的表中每个userid最后一条记录的方法》开发教程,希望对您有用。

《MysqL应用获取MysqL的表中每个userID最后一条记录的方法》要点:
本文介绍了MysqL应用获取MysqL的表中每个userID最后一条记录的方法,希望对您有用。如果有疑问,可以联系我们。

如下表:
MysqL学习

CREATE table `t1` (`userID` int(11) DEFAulT NulL,`atime` datetime DEFAulT NulL,KEY `IDx_userID` (`userID`)) ENGINE=InnoDB DEFAulT CHARSET=utf8; CREATE table `t1` (`userID` int(11) DEFAulT NulL,KEY `IDx_userID` (`userID`)) ENGINE=InnoDB DEFAulT CHARSET=utf8;

数据如下:
MysqL学习

MysqL> select * from t1;+--------+---------------------+| userID | atime |+--------+---------------------+| 1 | 2013-08-12 11:05:25 || 2 | 2013-08-12 11:05:29 || 3 | 2013-08-12 11:05:32 || 5 | 2013-08-12 11:05:34 || 1 | 2013-08-12 11:05:40 || 2 | 2013-08-12 11:05:43 || 3 | 2013-08-12 11:05:48 || 5 | 2013-08-12 11:06:03 |+--------+---------------------+8 rows in set (0.00 sec) MysqL> select * from t1;+--------+---------------------+| userID | atime |+--------+---------------------+| 1 | 2013-08-12 11:05:25 || 2 | 2013-08-12 11:05:29 || 3 | 2013-08-12 11:05:32 || 5 | 2013-08-12 11:05:34 || 1 | 2013-08-12 11:05:40 || 2 | 2013-08-12 11:05:43 || 3 | 2013-08-12 11:05:48 || 5 | 2013-08-12 11:06:03 |+--------+---------------------+8 rows in set (0.00 sec)

其中userID不唯一,要求取表中每个userID对应的时间离现在最近的一条记录.初看到一个这条件一般都会想到借用临时表及添加主建借助于join *** 作之类的.
给一个简方法:
MysqL学习

MysqL> select userID,substring_index(group_concat(atime order by atime desc),",1) as atime from t1 group by userID;+--------+---------------------+| userID | atime |+--------+---------------------+| 1 | 2013-08-12 11:05:40 || 2 | 2013-08-12 11:05:43 || 3 | 2013-08-12 11:05:48 || 5 | 2013-08-12 11:06:03 |+--------+---------------------+4 rows in set (0.03 sec) MysqL> select userID,1) as atime from t1 group by userID;+--------+---------------------+| userID | atime |+--------+---------------------+| 1 | 2013-08-12 11:05:40 || 2 | 2013-08-12 11:05:43 || 3 | 2013-08-12 11:05:48 || 5 | 2013-08-12 11:06:03 |+--------+---------------------+4 rows in set (0.03 sec)

Good luck!
MysqL学习

总结

以上是内存溢出为你收集整理的Mysql应用获取MySQL的表中每个userid最后一条记录的方法全部内容,希望文章能够帮你解决Mysql应用获取MySQL的表中每个userid最后一条记录的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/sjk/1160753.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存