1.查询每个用户最新的发言记录:
select max(time) from 2017sxgf group by id order by time desc limit 10;
2.找到发言数最多的用户ID和次数
select userid,count(userid) from orders where userid != '' group by userid order by count(userid) desc limit 1;
第一种是先排序,然后group,这样的话自然可以取到最适合的一条数据。
缺点很明显:Using temporary; Using filesort
select * from (select * from 2017sxgf order by time desc)t group by mobile limit 10;
第二种是联合查询
select * from (select max(time) as btime from 2017sxgf group by mobile limit 10)t left join 2017sxgf as s on t.btime = s.time;
第三种是子查询
select * from 2017sxgf where exists(select mobile from (select max(time) as btime from 2017sxgf group by mobile limit 10)t where t.btime = 2017sxgf.time);
5.
以上就是常用的sql语句的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号