是否可以在不使用限制的情况下从表中选择下一个较低的数字。
例如:如果我的表格10, 3, 2 , 1
我正在尝试select * from table where col > 10
。
我期待的结果是3
。我知道我可以使用限制1,但没有它可以完成吗?
答案 0 :(得分:6)
答案 1 :(得分:2)
尝试此查询
SELECT
*
FROM
(SELECT
@rid:=@rid+1 as rId,
a.*
FROM
tbl a
JOIN
(SELECT @rid:=0) b
ORDER BY
id DESC)tmp
WHERE rId=2;
| RID | ID | TYPE | DETAILS |
------------------------------------
| 2 | 28 | Twitter | @sqlfiddle5 |
另一种方法
select a.* from supportContacts a inner join
(select max(id) as id
from supportContacts
where
id in (select id from supportContacts where id not in
(select max(id) from supportContacts)))b
on a.id=b.id
| ID | TYPE | DETAILS |
------------------------------
| 28 | Twitter | @sqlfiddle5 |
答案 2 :(得分:1)
或者,此查询将始终根据内部where子句获得第二高的数字。
SELECT *
FROM
(
SELECT t.col,
(
SELECT COUNT(distinct t2.col)
FROM tableName t2
WHERE t2.col >= t.col
) as rank
FROM tablename t
WHERE col <= 10
) xx
WHERE rank = 2 -- <<== means second highest
答案 3 :(得分:-1)
如果你想从表中获得下一个较低的数字
你可以通过这个查询得到它:
SELECT distinct col FROM table1 a
WHERE 2 = (SELECT count(DISTINCT(b.col)) FROM table1 b WHERE a.col >= b.col);
稍后如果你想获得第三个较低的数字,你可以在where子句
中代替2来传递3如果你想得到第二个更高的数字,只需用
更改内部查询中where子句的条件a.col <= b.col