仅从SQL查询中获取几个值

仅从SQL查询中获取几个值,sql,Sql,我有一个SQL查询,它查找一些替换(由一些提供商提供)的价格,问题是我只需要那些以最低价格为我提供替换的提供商。(它们可以是1、2或更多) 我有这个查询,然后通过代码对其进行比较,但是有没有其他方法只使用SQL来完成我想要的任务 select * from tprovider_treplacement where replacement_id = ? order by price asc 多谢各位☺ 你想要这样的东西吗 select * from tprovider_treplacement

我有一个SQL查询,它查找一些替换(由一些提供商提供)的价格,问题是我只需要那些以最低价格为我提供替换的提供商。(它们可以是1、2或更多)

我有这个查询,然后通过代码对其进行比较,但是有没有其他方法只使用SQL来完成我想要的任务

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

多谢各位☺

你想要这样的东西吗

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

在末尾添加
limit 2
否,这是SQL,我不知道有多少提供商给我这些替换,可能是1,2,3,4,16…是的!!就这样!!!谢谢你,伙计,现在我看到了。。。没那么难。再次感谢^^