开发者

Query for find Nth maximum value in Mysql

How do I write a query to f开发者_高级运维ind the Nth maximum value in MySQL?


SELECT column FROM table ORDER BY column DESC LIMIT (n-1), 1

Have a look at http://php.about.com/od/mysqlcommands/g/Limit_sql.htm for reading up on LIMIT on SQL:

Definition: Limit is used to limit your MySQL query results to those that fall within a specified range. You can use it to show the first X number of results, or to show a range from X - Y results. It is phrased as Limit X, Y and included at the end of your query. X is the starting point (remember the first record is 0) and Y is the duration (how many records to display).


SELECT date FROM t1 WHERE date =(
SELECT DISTINCT(date ) FROM t1 AS SSELWS  WHERE 
(SELECT COUNT(DISTINCT(date))-1 FROM t1 )=
(
SELECT COUNT(DISTINCT(date ))+1 FROM t1 AS s2 WHERE SSELWS.date >s2.date ORDER BY s2.date ASC
)  ORDER BY SSELWS.date ASC)



Select * from world w1 where (N-1) = (Select Count(distinct(population)) from world w2 where w2.population > w1.population)



The Maximum Value for a Column:

SELECT MAX([column_name]) AS [alias] FROM [table_name];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜