2013-06-22 57 views
0

我有一個查詢某些替代品(由某些提供者提供)的價格的SQL查詢,問題是我只需要那些給我替換最小价格的提供者。 (他們可以是1,2或更多)從SQL查詢中只取幾個值

我有這個查詢,然後我通過代碼對它進行了修改,但是還有其他方法可以只用SQL來完成我想要的功能嗎?

select * from tprovider_treplacement where replacement_id = ? order by price asc 

謝謝你這麼多☺

+0

在末尾添加「限制2」 – Brian

+0

這是在Access中嗎? – Brad

+0

不,這是SQL,我不知道有多少供應商給我這些替換,可能是1,2,3,4,16 .... – Santanor

回答

1

你要這樣呢?

select * 
from tprovider_treplacement 
where replacement_id = ? and 
     price = (select min(price) 
       from tprovider_treplacement 
       replacement_id = ? 
      ) 

這是標準的SQL。許多數據庫還支持使用窗口函數的這種方法:

select * 
from (select pr.*, 
      min(price) over (partition by replacement_id) as minprice 
     from tprovider_treplacement pr 
     where replacement_id = ? 
    ) t 
where price = minprice 
+0

是的!而已!!!感謝哥們,現在我明白了......這並不難。再次感謝^^ – Santanor