我目前有兩個非常複雜的查詢,我使用這些查詢來引入評分。首先是所有的參與者和他們的階段成績,第二個是由每個參與者分組並計算他們的總體排名(階段總和)。mysql加入了兩個非常複雜的查詢
首先查詢以獲得每個階段的所有參與者的得分和排名
SELECT * FROM (
SELECT alias, catabbr, sh_dq, raw, sdq, dnf,
@rownum := IF(@stage != stage_name, 1, @rownum + 1) AS rowNum,
@rank := IF(@prevVal != cTime, @rownum, @rank) AS rank,
@stage := stage_name as stage_name,
@prevVal := cTime AS cTime,
id, zeroTime
FROM
(SELECT @rownum:= 0) rn,
(SELECT @rank:= 1) av,
(SELECT @stage:= '') sv,
(SELECT @prevVal:= 0) pv,
(SELECT s.stage_name stage_name, sc.alias alias, sc.catAbbr catabbr, sc.sh_dq sh_dq, sc.time raw, sc.id id, sc.sdq sdq, sc.dnf dnf,
IF(sc.time = "0", 1, 0) AS zeroTime,
((ROUND(sc.time, 2) + (sc.miss * 5)) + ((sc.proc * 10) + (sc.saf * 10) + (sc.sog * 30)) - (sc.bthPoints)) cTime
FROM matches m
LEFT JOIN stages s ON s.match_id = m.id
LEFT JOIN scores sc ON sc.stage_id = s.id
WHERE m.matchuuid = 'E13A4C61-A2B8-48E2-BE1B-1FFB77CC5849'
GROUP BY sc.alias, s.stage_name
ORDER BY s.stage_name, zeroTime, cTime
) tv
) t
ORDER BY zeroTime, alias, stage_name;
輸出:
+-----------+---------+-------+-----------------+-----+-----+--------+------+-----------------------+-------+----------+
| alias | catabbr | sh_dq | raw | sdq | dnf | rowNum | rank | stage_name | cTime | zeroTime |
+-----------+---------+-------+-----------------+-----+-----+--------+------+-----------------------+-------+----------+
| Back Bob | S | 0 | 52.9799995422 | 0 | 1 | 54 | 54 | Stage 1 | 92.98 | 0 |
+-----------+---------+-------+-----------------+-----+-----+--------+------+-----------------------+-------+----------+
| Back Bob | S | 0 | 43.9099998474 | 0 | 0 | 46 | 46 | Stage 2 | 48.91 | 0 |
+-----------+---------+-------+-----------------+-----+-----+--------+------+-----------------------+-------+----------+
| Ben Scal | ES | 0 | 26.9699993134 | 0 | 0 | 27 | 27 | Stage 1 | 31.97 | 0 |
+-----------+---------+-------+-----------------+-----+-----+--------+------+-----------------------+-------+----------+
| Ben Scal | ES | 0 | 32.8800010681 | 0 | 0 | 38 | 38 | Stage 2 | 42.88 | 0 |
+-----------+---------+-------+-----------------+-----+-----+--------+------+-----------------------+-------+----------+
二按名稱查詢組參與者,總結他們的隊伍作最後的排名
SELECT alias, catabbr, SUM(cTime) fTime, SUM(rank) fRank, zeroTime, rank
FROM (
SELECT alias, catabbr, sh_dq, raw, sdq, dnf,
@rownum := IF(@stage != stage_name, 1, @rownum + 1) AS rowNum,
@rank := IF(@prevVal != cTime, @rownum, @rank) AS rank,
@stage := stage_name as stage_name,
@prevVal := cTime AS cTime,
id, zeroTime
FROM
(SELECT @rownum:= 0) rn,
(SELECT @rank:= 1) av,
(SELECT @stage:= '') sv,
(SELECT @prevVal:= 0) pv,
(SELECT s.stage_name stage_name, sc.alias alias, sc.catAbbr catabbr, sc.sh_dq sh_dq, sc.time raw, sc.id id, sc.sdq sdq, sc.dnf dnf,
IF(sc.time = "0", 1, 0) AS zeroTime,
((ROUND(sc.time, 2) + (sc.miss * 5)) + ((sc.proc * 10) + (sc.saf * 10) + (sc.sog * 30)) - (sc.bthPoints)) cTime
FROM matches m
LEFT JOIN stages s ON s.match_id = m.id
LEFT JOIN scores sc ON sc.stage_id = s.id
WHERE m.matchuuid = 'E13A4C61-A2B8-48E2-BE1B-1FFB77CC5849'
GROUP BY sc.alias, s.stage_name
ORDER BY s.stage_name, zeroTime, cTime
) tv
) t
GROUP BY alias
ORDER BY zeroTime, fRank, fTime;
輸出:
+-----------+-----------+-----------+-------+-----------+
| alias | catabbr | fTime | fRank | zeroTime |
+-----------+-----------+-----------+-------+-----------+
| Back Bob | S | 141.89 | 100 | 0 |
+-----------+-----------+-----------+-------+-----------+
| Ben Scal | ES | 74.85 | 68 | 0 |
+-----------+-----------+-----------+-------+-----------+
zeroTime列使得我可以按大於0的時間排序,並正確計算排名。
當我嘗試加入他們時,我沒有得到任何結果或任何東西。有沒有辦法將這兩個查詢合併/連接成一個查詢,以便他們輸出以下內容?
+-----------+-----------+-----------+-------+-----------+---------------+---------------+---------------+---------------+
| alias | catabbr | fTime | fRank | zeroTime | Stage 1 Time | Stage 1 Rank | Stage 2 Time | Stage 2 Rank |
+-----------+-----------+-----------+-------+-----------+---------------+---------------+---------------+---------------+
| Ben Scal | ES | 74.85 | 68 | 0 | 31.97 | 27 | 42.88 | 38 |
+-----------+-----------+-----------+-------+-----------+---------------+---------------+---------------+---------------+
| Back Bob | S | 141.89 | 100 | 0 | 92.98 | 54 | 48.91 | 46 |
+-----------+-----------+-----------+-------+-----------+---------------+---------------+---------------+---------------+
這將進入一個PHP頁面,所以我可以遍歷和我需要什麼拉,我試圖做的主要事情是加入這兩個查詢。
我的結構和數據對於sqlfiddle來說太大了。 這裏是比賽和階段結構和插入:http://pastebin.com/YvZevd5j 下面是成績結構和插入:http://pastebin.com/jLBYxMvc
有一點需要注意得是,一場比賽可以有1個或多個階段,1個或多個particpants。
這將工作,如果我事先知道多少個階段,但我不這樣它必須是動態的。我已經提前找到了一種使用這兩種查詢並通過php合併數據的方法。儘管感謝您的建議。 – codivist 2012-08-16 18:10:02