MySQL 如何选择一列数值最低的一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4581570/
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 18:11:04  来源:igfitidea点击:

How to select one row with lowest numeric value of one column?

mysql

提问by Apic

What would be the query to select one row with lowest numeric value of one column?

选择一列数值最低的一行的查询是什么?

回答by ughoavgfhw

You can use it

你可以使用它

SELECT * FROM `YourTable` ORDER BY YourColumn LIMIT 1

回答by Mike Hillyer

try this

尝试这个

SELECT * FROM `table` ORDER BY `column` ASC LIMIT 1;

回答by Jon Black

select
 f.*
from
 foo f
inner join
(
 select min(id) as id from foo
) m on m.id = f.id;

回答by nycdan

To select all rows that match the minimum value for one column (in SQL Server)

选择与一列的最小值匹配的所有行(在 SQL Server 中)

SELECT T.col1, T.col2  
From Table T  
Where T.col1 = (select MIN(col1) from Table) 

To select only one row, you could modify the first line to be:

要仅选择一行,您可以将第一行修改为:

Select Top 1 T.col1, T.col2

and you can always add 'Order by colx'to the end (multiple columns, comma separated work too).

并且您始终可以在末尾添加“Order by colx”(多列,逗号分隔的工作也是如此)。

Hope that helps.

希望有帮助。

回答by Sergio del Amo

I was looking for a simliar solution. I wanted the complete row with a minium value of a table group by another column. Here is the solution I came across. I sorted the table by the minium column with a order_by clause and the feed the subquery to a query with ORDER BY which catches the first row which appears which is the sorted row.

我正在寻找类似的解决方案。我想要一个表组的最小值的完整行由另一列组成。这是我遇到的解决方案。我使用 order_by 子句按 minium 列对表进行排序,并将子查询提供给使用 ORDER BY 的查询,该查询捕获出现的第一行,即已排序的行。

id  bigint(20)      
commission  decimal(5,4)
door_price  decimal(19,2)
event_id    bigint(20)
min_commission  decimal(19,2)
price   decimal(19,2)
visible_to_public

SELECT * FROM (SELECT       
        price_bundle.id as id,
        event_id,
        price_bundle.price + (case when ((price_bundle.commission * price_bundle.price) >  price_bundle.min_commission) then (price_bundle.commission * price_bundle.price) else price_bundle.min_commission end) AS total
     FROM price_bundle
 WHERE price_bundle.visible_to_public = 1
 ORDER BY total asc
) AS price_bundle_order_by_total_price_asc GROUP BY event_id