2013-06-22 54 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