2016-02-20 43 views
0

鉴于类似下面的表格:选择行

id  quantity 
1  5 
2  3 
3  7 
4  2 
5  8 

给定一个id和秩序,我想选择会来给定id毕竟行如果下令在给定订购。例如,如果ID为1,并依次为“量递减”,我想

id quantity 
2  3 
4  2 

我怎样才能做到这一点只用SQL。我只能想到一种结合php和sql的方式。

回答

1

您可以使用此子查询:

select t.* 
from t 
where quantity < (select t2.quantity from t t2 where t2.id = 1) 
order by quantity desc;