具有列的最小值的行

时间:2010-10-01 14:15:54

标签: sql sql-server

有这个选择:

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

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

7 个答案:

答案 0 :(得分:20)

select * from TABLE_NAME order by COLUMN_NAME limit 1

答案 1 :(得分:17)

我试试这个:

SELECT TOP 1 *
FROM TABLE1
ORDER BY N_UM

(使用SQL Server)

答案 2 :(得分:16)

试试这个 -

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

答案 3 :(得分:6)

使用此sql查询:

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

答案 4 :(得分:4)

方法1:

import datetime
import webapp2

class MainPage(webapp2.RequestHandler):
  def get(self):
    message = '<p>The time is: %s</p>' % datetime.datetime.now()
    self.response.out.write(message)

application = webapp2.WSGIApplication([('/', MainPage)], debug=True)

方法2:

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

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

方法3:

SELECT * 
FROM table 
ORDER BY N_UM 
LIMIT 1

答案 5 :(得分:1)

这是一种方法

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 )

答案 6 :(得分:0)

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