我是 SQL 新手,我想在不使用 LIMIT 的情况下获取第 5 个最高值。这是我正在尝试的代码,但它无法正常工作。它显示的是第 5 个最低值,而不是第 5 个最高值。
SELECT a . * FROM user AS a
WHERE 5 =
(SELECT count( DISTINCT b.id ) FROM user AS b WHERE b.id >= a.id ORDER BY a.id DESC)
谁能帮我这个?
你也可以这样做:
SET @nth := 5;
SELECT
a.*
FROM jos_modules AS a
WHERE @nth = (
SELECT
COUNT(b.id)
FROM user AS b
WHERE
a.id >= b.id
);
尝试这个
SELECT a . *
FROM user AS a
WHERE 5 = (
SELECT count( DISTINCT b.id )
FROM user AS b
WHERE a.id >= b.id ORDER BY a.id )
select * from (
select a.* , row_number() over (order by id asc) as RANK
from a ) where RANK=5 ;
如果您使用的是 Teradata DB,则可以使用限定语句:
select * from a
qualify row_number () over(order by id asc)=5;