2016-02-20 26 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;