SQL 列的最小值的行

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

row with minimum value of a column

sqlsql-server

提问by mtz

Having this selection:

有这个选择:

id IDSLOT  N_UM
------------------------
1  1  6
2  6  2
3  2  1
4  4  1
5  5  1
6  8  1
7  3  1
8  7  1
9  9  1
10  10  0

I would like to get the row (only one) which has the minimun value of N_UM, in this case the row with id=10 (10 0).

我想获得具有 N_UM 最小值的行(只有一个),在这种情况下是 id=10 (10 0) 的行。

采纳答案by Sachin Shanbhag

Try this -

尝试这个 -

 select top 1 * from table where N_UM = (select min(N_UM) from table);

回答by Andrejs Cainikovs

select * from TABLE_NAME order by COLUMN_NAME limit 1

回答by Adam V

I'd try this:

我会试试这个:

SELECT TOP 1 *
FROM TABLE1
ORDER BY N_UM

(using SQL Server)

(使用 SQL Server)

回答by Kai Sternad

Use this sql query:

使用这个 sql 查询:

select id,IDSLOT,N_UM from table where N_UM = (select min(N_UM) from table));

回答by Ehsan

Method 1:

方法一:

SELECT top 1 * 
FROM table 
WHERE N_UM = (SELECT min(N_UM) FROM table);

Method 2:

方法二:

SELECT * 
FROM table 
ORDER BY N_UM 
LIMIT 1

A more general solution to this class of problem is as follows.

此类问题的更一般解决方案如下。

Method 3:

方法三:

SELECT *
FROM table 
WHERE N_UM IN (SELECT MIN(N_UM) FROM table);

回答by Sage

Here is one approach

这是一种方法

Create table #t (
id int,
IDSLOT int,
N_UM int
)
insert into #t ( id, idslot, n_um )
VALUES (1, 1, 6),
 (2,6,2),
 (3,2,1),
 (4,4,1),
 (5,5,1),
 (6,8,1),
 (7,3,1),
 (8,7,1),
 (9,9,1),
 (10, 10, 0)

 select Top 1 *
 from #t
 Where N_UM = ( select MIN(n_um) from #t )

回答by Ankur vijay

select TOP 1  Col , COUNT(Col) as minCol from employee GROUP by Col
order by mindep  asc