MySQL 如何在MySQL中选择字段具有最小值的数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13357144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:26:40  来源:igfitidea点击:

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

mysqlsqlaggregate-functionsgreatest-n-per-group

提问by Sami El Hilali

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

我想从 MySQL 中的表中选择数据,其中特定字段具有最小值,我试过这个:

SELECT * FROM pieces WHERE MIN(price)

Please any help?

请任何帮助?

回答by John Woo

this will give you result that has the minimum price on all records.

这将为您提供所有记录中最低价格的结果。

SELECT *
FROM pieces
WHERE price =  ( SELECT MIN(price) FROM pieces )

回答by sberry

This is how I would do it (assuming I understand the question)

这就是我会怎么做(假设我理解这个问题)

SELECT * FROM pieces ORDER BY price ASC LIMIT 1

If you are trying to select multiple rows where each of them may have the same price (which is the minimum) then @JohnWoo's answer should suffice.

如果您尝试选择多行,其中每行的价格可能相同(这是最低价格),那么@JohnWoo 的回答就足够了。

Basically here we are just ordering the results by the price in ASCending order (increasing) and taking the first row of the result.

基本上在这里,我们只是按 ASCending 顺序(递增)中的价格对结果进行排序,并取结果的第一行。

回答by pulsit

Use HAVING MIN(...)

使用HAVING MIN(...)

Something like:

就像是:

SELECT MIN(price) AS price, pricegroup
FROM articles_prices
WHERE articleID=10
GROUP BY pricegroup
HAVING MIN(price) > 0;

回答by Daniel Rodriguez

In fact, depends what you want to get: - Just the min value:

事实上,取决于你想得到什么: - 只是最小值:

SELECT MIN(price) FROM pieces
  • A table (multiples rows) whith the min value: Is as John Woo said above.

  • But, if can be different rows with same min value, the best is ORDER them from another column, because after or later you will need to do it (starting from John Woo answere):

    SELECT * FROM pieces WHERE price = ( SELECT MIN(price) FROM pieces) ORDER BY stock ASC

  • 具有最小值的表(多行):正如 John Woo 上面所说。

  • 但是,如果可以是具有相同最小值的不同行,最好是从另一列订购它们,因为之后或之后您将需要这样做(从 John Woo answere 开始):

    SELECT * FROMpieces WHERE price = (SELECT MIN(price) FROMpieces) ORDER BY stock ASC

回答by fthiella

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)

(因为这个版本没有带子查询的 where 条件,如果你需要更新表可以使用它,但如果你只需要 SELECT 我会推荐使用 John Woo 解决方案)

回答by Vijay Maurya

Efficient way (with any number of records):

有效的方式(具有任意数量的记录):

SELECT id, name, MIN(price) FROM (select * from table order by price) as t group by id

回答by Mark Valenzuela

To make it simpler

为了让它更简单

SELECT *,MIN(price) FROM prod LIMIT 1

SELECT *,MIN(price) FROM prod LIMIT 1

  • Put * so it will display the all record of the minimum value
  • 把*这样它会显示最小值的所有记录