How to select data where a field has a min value in MySQL?

后端 未结 7 1299
半阙折子戏
半阙折子戏 2020-12-02 17:02

I want to select data from a table in MySQL where a specific field has the minimum value, I\'ve tried this:

SELECT * FROM pieces WHERE MIN(price)
         


        
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 17:30

    This also works:

    SELECT
      pieces.*
    FROM
      pieces inner join (select min(price) as minprice from pieces) mn
      on pieces.price = mn.minprice
    

    (since this version doesn't have a where condition with a subquery, it could be used if you need to UPDATE the table, but if you just need to SELECT i would reccommend to use John Woo solution)

提交回复
热议问题