SQL查询最大值,返回整行数据
1、问题
部分数据如下,未完整展示。如何从 table_a 表中查询 p_postions 列的最大值对应的一行数据?

2、解答
方法1:先排序,再取第一条
SELECT * FROM table_a order by p_postions desc limit 1;
方法2:先查最大值,再找到对应行(推荐)
参考:https://stackoverflow.com/questions//selecting-a-record-with-max-value
这是一种时间复杂度为 O(n) 的方法:
SELECT * FROM table_a WHERE p_postions = (SELECT MAX(p_postions) FROM table_a) LIMIT 1;
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/214608.html原文链接:https://javaforall.net
