2012-03-03 104 views
0

我計算MINMAX和玩家等級的AVGMySQL的計算與價值

SELECT 
    MIN(`p`.`level`) AS 'min', 
    MAX(`p`.`level`) AS 'max', 
    AVG(`p`.`level`) AS 'avg' 
FROM `dfh`.`players` `p` 

如何?我怎樣才能它指的是MINMAXp.name字段值?

Fe。我在p表得到這個值:

Lp. | Name | Level 
1 | Test | 12 
2 | Tesx | 15 
3 | Anot | 150 

和查詢將退出以下內容:

MIN: 12 
MAX: 150 

但我希望它也撤回Name對應MINMAX值:

MIN: 12 Test 
MAX: 150 Anot 

我該怎麼做?

+0

MySQL有沒有遺憾的分析功能,所以你必須寫一個漫長而枯燥的查詢。 – Benoit 2012-03-03 12:49:55

+0

@Benoit「UNION」的大型連鎖店很無聊嗎? – 2012-03-03 12:55:52

+0

@Michael::)我應該解釋一下無聊是什麼。對我而言,任何需要高劑量複製粘貼的代碼都有點無聊。在這些聯盟中,只有聚合函數發生變化! – Benoit 2012-03-03 12:59:34

回答

2

嘗試此查詢 -

SELECT 'MIN' property, GROUP_CONCAT(p1.name) name, p1.level FROM players p1 
    JOIN (SELECT MIN(level) level FROM players) p2 
    ON p1.level = p2.level 
UNION 
SELECT 'MAX' property, GROUP_CONCAT(p1.name) name, p1.level FROM players p1 
    JOIN (SELECT MAX(level) level FROM players) p2 
    ON p1.level = p2.level 

查詢最高的「體驗」的價值和任何「身份證」 -

SELECT * FROM 

    (SELECT 'MIN' property, p1.id, p1.level, p1.name FROM players p1 
    JOIN (
     SELECT 
     level, MAX(experience) experience 
     FROM 
     players, 
     (SELECT @level:=MIN(level) FROM players) t 
     WHERE level = @level 
    ) p2 
    ON p1.experience = p2.experience AND p1.level = p2.level 
    ORDER BY id LIMIT 1) t_min 

UNION 

SELECT * FROM 

    (SELECT 'MAX' property, p1.id, p1.level, p1.name FROM players p1 
    JOIN (
     SELECT 
     level, MAX(experience) experience 
     FROM 
     players, 
     (SELECT @level:=MAX(level) FROM players) t 
     WHERE level = @level 
    ) p2 
    ON p1.experience = p2.experience AND p1.level = p2.level 
    ORDER BY id LIMIT 1) t_max 

使用該數據集作爲一個子查詢的加盟與另一個表 -

SELECT * FROM 
(
    SELECT * FROM 

    (SELECT 'MIN' property, p1.id, p1.level, p1.name FROM players p1 
     JOIN (
     SELECT 
      level, MAX(experience) experience 
     FROM 
      players, 
      (SELECT @level:=MIN(level) FROM players) t 
     WHERE level = @level 
    ) p2 
     ON p1.experience = p2.experience AND p1.level = p2.level 
    ORDER BY id LIMIT 1) t_min 

    UNION 

    SELECT * FROM 

    (SELECT 'MAX' property, p1.id, p1.level, p1.name FROM players p1 
     JOIN (
     SELECT 
      level, MAX(experience) experience 
     FROM 
      players, 
      (SELECT @level:=MAX(level) FROM players) t 
     WHERE level = @level 
    ) p2 
     ON p1.experience = p2.experience AND p1.level = p2.level 
    ORDER BY id LIMIT 1) t_max 
) p 
LEFT JOIN guild_member gm 
    ON gm.id = p.id 
+0

它可以正常工作,但如果有兩名同級玩家呢?在這種情況下,應該選擇其中的一個,atm。它會選擇所有具有相同級別的玩家。 – Cyclone 2012-03-03 13:30:41

+0

他們應該選擇哪一個? – Devart 2012-03-03 13:38:01

+0

嗯,還有一個'經驗'字段,所以也許如果有兩個玩家(或更多)具有相同的等級,我們應該選擇'經驗'字段最高的那個(類似於'ORDER BY p.experience DESC' )。 – Cyclone 2012-03-03 13:43:23

1

這將返回低/高名有效

select 'MIN', level, name from players order by level limit 1 
union all 
select 'MAX', level, name from players order by level desc limit 1 

通知缺少子查詢......

+0

[錯誤] 1221 - UNION和ORDER BY的用法不正確 – Cyclone 2012-03-03 14:09:38