简体   繁体   English

在所有行的新列中选择全局min()

[英]Select global min() in a new column for all rows

I have a table db_race 我有一张桌子db_race

--------------------------
| Name    | Time | Map   |
--------------------------
| Max     | 240  | test1 |
| Alvin   | 600  | test2 |
| Amanda  | 234  | test1 |
| Angela  | 50   | test1 |
| Angela  | 2000 | test1 |
--------------------------

I now want to select some rows ordered by best Time on a map like the following 我现在想在地图上选择按最佳时间排序的一些行,如下所示

SELECT Name, min(Time) AS Time 
FROM db_race 
WHERE Map='test1' 
GROUP BY Name 
ORDER BY Time ASC

That works perfectly fine, but I want to have the best time of all records added to another column looking like the following. 这样做很好,但是我希望将所有记录中的最佳时间添加到另一列中,如下所示。 (will later work with that but thats not a part of the question) (以后可以使用,但那不是问题的一部分)

----------------------------
| Name    | Time | TopTime |
----------------------------
| Angela  | 50   | 50      |
| Amanda  | 234  | 50      |
| Max     | 240  | 50      |
----------------------------

So far I tried the following but it only shows the best time, I want it for the whole selected table 到目前为止,我尝试了以下操作,但它只显示了最佳时间,我希望整个选定表都可以使用

SELECT *, min(Time) AS TopTime 
FROM (SELECT Name, min(Time) AS Time 
      FROM db_race 
      WHERE Map='test1' 
      GROUP BY Name 
      ORDER BY Time ASC) a

I want to make sure I dont need to find the TopTime by using a subquery. 我想确保我不需要通过使用子查询来找到TopTime。 The actual query will be very long and my goal is to not have it executed twice. 实际查询将很长,我的目标是不要执行两次。

Something like this, perhaps? 大概是这样吗?

SELECT 
    Name, 
    min(Time) as Time, 
    (SELECT min(Time) FROM db_race WHERE Map='test1') as TopTime
FROM db_race
WHERE Map='test1'
GROUP BY Name
ORDER BY Time

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM