2011-10-19 46 views
1

例如X数据:如果我有其中包含一个表:我怎样才能在一个SELECT查询

PRICES 
1 
5 
3 
8 
2 
8 

如果我想的第二个元素,我怎样才能得到它呢?只有那个数字..有可能吗?

回答

1

试试这个:

declare @x int 
set @x = 3 

select top 1 
from (select top @x from table order by 1 desc) xx 
0
$third_element = mysql_result(mysql_query("SELECT prices FROM the_table WHERE prices = '3'"), 0); 

,其选择的元素,但我不知道你为什么会想那样做,除非你有另一行从等,其中other_row =“东西”,那么你会在抵达选择它3.

0

是有可能 这里是你的答案的解决方案尝试。

create table prices 
(
    price int 
) 

insert into prices values (1) 
insert into prices values (5) 
insert into prices values (3) 
insert into prices values (8) 
insert into prices values (2) 
insert into prices values (8) 

select x.* from 
(
    select ROW_NUMBER()over(order by price) as RowNumber,price 
    from prices 
)x 
where x.RowNumber=3