表的例子结构:
tbltest4:
id
23
1
2
3
4
5
6
7
8
9
55
76
1
2
3
4
5
6
7
8
9
55
76
1、选取最大的数
select
max(id)
from tbltest4
2、选取最大的三个数
select
top 3 *
from tbltest4
order
by id
desc
3、选取第2大的数
select
top 1 a.*
from (
select
top 2 *
from tbltest4
order
by id
desc) a
order
by id
asc
或者
select *
from tbltest4
where id=
(
select max(id) from tbltest4 where id not in
( select max(id) from tbltest4)
)
(
select max(id) from tbltest4 where id not in
( select max(id) from tbltest4)
)
4、选取第3大的数
select
top 1 a.*
from (
select
top 3 *
from tbltest4
order
by id
desc) a
order
by id
asc
5、选取最小的数
select
min(id)
from tbltest4
6、选取第2小的数
select
top 1 a.*
from (
select
top 2 *
from tbltest4
order
by id
asc) a
order
by id
desc
或
select *
from tbltest4
where id=
(
select min(id) from tbltest4 where id not in
( select min(id) from tbltest4)
)
(
select min(id) from tbltest4 where id not in
( select min(id) from tbltest4)
)
7、选取第3小的数
select
top 1 a.*
from (
select
top 3 *
from tbltest4
order
by id
asc) a
order
by id
desc
本文转自xudayu 51CTO博客,原文链接:http://blog.51cto.com/xudayu/86281,如需转载请自行联系原作者