从一列获取MAX值,从另一列获取MIN

问题描述 投票:4回答:4

我一直在制作一款基于Candy Crush的游戏。 Score表有以下三列:

stage_level_id                        | value | moves
------------------------------------------------------
9f7678f0-fc8f-11e3-a398-b2227cce2b53  | 35000 | 350
9f7678f0-fc8f-11e3-a398-b2227cce2b53  | 35000 | 500
9f7678f0-fc8f-11e3-a398-b2227cce2b54  | 15000 | 125
9f7678f0-fc8f-11e3-a398-b2227cce2b54  | 13500 | 100
9f7678f0-fc8f-11e3-a398-b2227cce2b55  | 12500 | 350
9f7678f0-fc8f-11e3-a398-b2227cce2b55  | 7500  | 25

我需要得到stage_level_id分组的最高分。如果stage_level_id具有相同的Value(以53结尾的那个),它必须返回具有最小数量的Moves的行。

我正在尝试以下但它没有按预期工作:

SELECT a.stage_level_id, MAX(a.value) as max_value, a.moves
FROM scores a
LEFT JOIN scores b ON (
  a.stage_level_id = b.stage_level_id
)
RIGHT JOIN scores c ON (
  c.moves = ( SELECT MIN(moves) as moves FROM scores WHERE c.stage_level_id =         a.stage_level_id )
)
WHERE a.player_id = 1475332386040815
GROUP BY a.stage_level_id

预期的结果是:

stage_level_id                        | value | moves
------------------------------------------------------
9f7678f0-fc8f-11e3-a398-b2227cce2b53  | 35000 | 350
9f7678f0-fc8f-11e3-a398-b2227cce2b54  | 15000 | 125
9f7678f0-fc8f-11e3-a398-b2227cce2b55  | 12500 | 350

我做错了什么?

mysql
4个回答
2
投票

你的尝试并没有那么遥远。您错过了第一个JOIN ... ON子句的必要部分,但第二个JOIN不是必需的。

SELECT tbl1.stage_level_id, tbl1.max_value, MIN(s.moves) AS moves
FROM 
(
  SELECT stage_level_id, MAX(value) AS max_value
  FROM scores
  GROUP BY stage_level_id
) tbl1
LEFT JOIN scores s ON tbl1.stage_level_id = s.stage_level_id AND tbl1.max_value = s.value
GROUP BY stage_level_id

DEMO


2
投票

您可以使用NOT EXISTS获取每个(stage_id,max score)组的最小移动次数

select stage_level_id, max(value), min(moves)
from scores s1
where not exists (
    select 1 from scores s2
    where s2.stage_level_id = s1.stage_level_id
    and s2.value > s1.value
)
group by stage_level_id;

not exists部分将结果限制为仅在每个组中具有最高分数的行(换句话说,不存在组内具有更高分数的其他行)。

此查询可以利用(stage_id,value)上的复合索引

http://sqlfiddle.com/#!2/88ee6/8


0
投票

不确定我是否误解了这个问题,但不是这么简单:

SELECT column1, max(column2), IF(column1 like "%53", min(column3), max(column3))
FROM test
GROUP BY column1
;

看看这个,SQLFiddle link


0
投票

事实上,这可以通过一个selectleft joingroup by简单地完成:

select s1.stage_level_id, s1.score, min(s1.moves) as moves
from scores s1
  left join scores s2
    on s2.stage_level_id = s1.stage_level_id
    and s2.score > s1.score
where s2.score IS NULL
group by s1.stage_level_id;

left join给出:

| s1                                                   | s2                                                     |
|--------------------------------------|-------|-------|--------------------------------------|--------|--------|
| stage_level_id                       | score | moves | stage_level_id                       | score  | moves  |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b53 | 35000 | 350   | (null)                               | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b53 | 35000 | 500   | (null)                               | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b54 | 15000 | 125   | (null)                               | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b54 | 13500 | 100   | 9f7678f0-fc8f-11e3-a398-b2227cce2b54 | 15000  | 125    |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b55 | 7500  | 25    | 9f7678f0-fc8f-11e3-a398-b2227cce2b55 | 12500  | 350    |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b55 | 12500 | 350   | (null)                               | (null) | (null) |

where子句选择每个stage_level_id包含最高分数的行:

| s1                                                   | s2                               |
|--------------------------------------|-------|-------|----------------|--------|--------|
| stage_level_id                       | score | moves | stage_level_id | score  | moves  |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b53 | 35000 | 350   | (null)         | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b53 | 35000 | 500   | (null)         | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b54 | 15000 | 125   | (null)         | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b55 | 12500 | 350   | (null)         | (null) | (null) |

这样我们就可以在min(s1.moves)制定的每个stage_level_id组上运行group by来获得最终结果:

| s1                                                   | s2                               |
|--------------------------------------|-------|-------|----------------|--------|--------|
| stage_level_id                       | score | moves | stage_level_id | score  | moves  |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b53 | 35000 | 350   | (null)         | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b54 | 15000 | 125   | (null)         | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b55 | 12500 | 350   | (null)         | (null) | (null) |

要么

| stage_level_id                       | score | moves |
|--------------------------------------|-------|-------|
| 9f7678f0-fc8f-11e3-a398-b2227cce2b53 | 35000 | 350   |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b54 | 15000 | 125   |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b55 | 12500 | 350   |

http://sqlfiddle.com/#!9/50ed8/6/0测试一下。

© www.soinside.com 2019 - 2024. All rights reserved.