MySQL替代T-SQL WITH TIES

时间:2010-08-12 15:02:59

标签: sql mysql

我有一张表,我希望从中获得前N个记录。记录按值排序,某些记录具有相同的值。我想在这里做的是获得前N个记录的列表,包括绑定的记录。这就是表格中的内容:

+-------+--------+
| Name  | Value  |
+-------+--------+
| A     | 10     |
| B     | 30     |
| C     | 40     |
| D     | 40     |
| E     | 20     |
| F     | 50     |
+-------+--------+

现在,如果我想得到前三名

SELECT * FROM table ORDER BY Value DESC LIMIT 3

我明白了:

+-------+--------+
| Name  | Value  |
+-------+--------+
| F     | 50     |
| C     | 40     |
| D     | 40     |
+-------+--------+

我想得到的是这个

+-------+--------+
| Name  | Value  |
+-------+--------+
| F     | 50     |
| C     | 40     |
| D     | 40     |
| B     | 30     |
+-------+--------+

我计算每条记录的等级,所以我真正想要的是获得前N个记录而不是按值排序的前N个记录。这是我计算排名的方式:

SELECT Value AS Val, (SELECT COUNT(DISTINCT(Value))+1 FROM table WHERE Value > Val) as Rank

在T-SQL中,这样做是可以实现的:

SELECT TOP 3 FROM table ORDER BY Value WITH TIES

有没有人知道如何在MySQL中执行此操作?我知道它可以用子查询或临时表来完成,但我没有足够的知识来完成这个。我更喜欢不使用临时表的解决方案。

3 个答案:

答案 0 :(得分:6)

这对你有用吗?

select Name, Value from table where Value in (
    select distinct Value from table order by Value desc limit 3
) order by Value desc

或者也许:

select a.Name, a.Value 
from table a
join (select distinct Value from table order by Value desc limit 3) b
     on a.Value = b.Value

答案 1 :(得分:3)

select a.Name, a.Value 
from table a
join (select Value from table order by Value desc limit 3) b
     on a.Value = b.Value

这就像@Fosco的答案,但子查询中没有DISTINCT。他的版本返回前N分的球员,而不是前N名球员(加上领带)。例如。如果分数分别为50,50,50,40,40,30,20,他将返回6名玩家(3x50,2x40,1x30),但你可能只想要3x50。

答案 2 :(得分:0)

Starting with MySQL 8, you can use window functions to emulate the WITH TIES semantics, by filtering on RANK(). For example:

SELECT Name, Value
FROM (
  SELECT Name, Value, RANK() OVER (ORDER BY Value DESC) AS rk
  FROM table
) t
WHERE rk <= 3

Note that when reading your question more closely, this doesn't do exactly what you seem to want, but it does exactly what T-SQL can do through the TOP n WITH TIES clause.