2013-03-24 32 views
0

我正在查找一條語句以查找具有最小特殊字段值的用戶。 我的意思是這樣查找表格的最小值

Select ID, Username, Joindate, MIN(score) 
from table1 

其實我在尋找一種方式來找到得分最低的用戶。

回答

2

要找到得分最低的用戶,你可以排序的simpy表,並採取的第一條記錄:

SELECT TOP 1 ID, UserName, JoinDate, score FROM table1 ORDER BY score 
1

可以將查詢 -

Select ID,Username,Joindate,score from table1 
where score in (select MIN(score) from table1) 

感謝

2

你可以通過幾種不同的方式得到這個結果。

子查詢:

Select t1.ID, 
    t1.Username, 
    t1.Joindate, 
    t1.Score 
from table1 t1 
inner join 
(
    select min(score) LowestScore 
    from table1 
) t2 
    on t1.score = t2.lowestscore 

TOP WITH TIES

select top 1 with ties id, username, joindate, score 
from table1 
order by score 

你甚至可以使用ranking functions得到結果:

select id, username, joindate, score 
from 
(
    select id, username, joindate, score, 
    rank() over(order by score) rnk 
    from table1 
) src 
where rnk = 1 

查看所有查詢SQL Fiddle with Demo

這些都會返回所有用戶得分最低的用戶。

0

從table1中選擇top 1 ID,用戶名,Joindate,得分 ,其中score =(從table1中選擇min(分數))