2015-01-09 62 views
-1

我目前這個問題所困擾,我需要獲得最新的連續輸贏成員的結果得到連續輸多贏結果

tbl_results 
result_id member_id result match_date 
1   1   W  2014-12-28 
2   1   W  2014-12-21 
3   1   W  2014-12-14 
4   1   L  2014-12-17 
5   1   W  2014-11-30 
6   2   L  2014-12-28 
7   2   L  2014-12-21 
8   2   L  2014-12-14 
9   2   W  2014-12-17 
10   2   L  2014-11-30 
11   3   W  2014-12-28 
12   3   L  2014-12-21 
13   3   W  2014-12-14 
14   3   W  2014-12-17 
15   3   W  2014-11-30 

很多時間在互聯網研究後,我來了了這個

SET @mID = 0; 
SET @num = 1; 

//this variable will hold what to count 
SET @wtc = 'W'; 
SET @wl = 1; 

SET @consecutives = 'FALSE'; 

SELECT 
    tbl_results.member_id, 

    //I add here a row number for each member_id so that i can determine if the iteration has changed members or not 
    @num := IF(@mID = tbl_results.member_id, @num + 1, 1) AS row_number, 
    @mID := tbl_results.member_id AS temp_m_id, 

    //from here onwards, I started using some programming syntax and logic 
    //I plan to convert it later to the correct MySQL syntax 

    //new member detected 
    IF @num = 1 THEN 
     //determines what to count (the wins or the loses) 
     @wtc := IF(tbl_results.remarks = 'W', 'W', 'L') AS what_to_count, 
     //consecutives started 
     SET @consecutives = 'TRUE'; 
     DISPLAY 1 in win_lose column 
    ELSE 
     //if it is still consecutives 
     iF @consecutives = 'TRUE' THEN 
      IF @wtc = 'W' THEN 
       IF tbl_results.remarks = 'W' THEN 
        DISPLAY 1 in win_lose column 
       ELSE 
        //consecutive wins has been broken 
        @consecutives = 'FALSE'; 
        DISPLAY 0 in win_lose column 
       END IF 
      ELSE 
       IF tbl_results.remarks = 'L' THEN 
        DISPLAY 1 in win_lose column 
       ELSE 
        //consecutive loses has been broken 
        consecutives = 'FALSE'; 
        DISPLAY 0 in win_lose column 
       END IF 
      END IF 
     ELSE 
      DISPLAY 0 in win_lose column here up to the next member 
     END IF 
    END IF 
FROM 
tbl_results 

我知道,上面的查詢肯定是不對的,我沒有經歷過用mysql變量,我剛剛看了一下前一段時間,但我計算過,我告訴你我」如果您有任何建議,請聯繫我們

我所用我上面的查詢目標是輸出這樣的

member_id row_number temp_m_id  win_lose  what_to_count 
1   1    1    1   W 
1   2    1    1 
1   3    1    1 
1   4    1    0 
1   5    1    0 
2   1    2    1   L 
2   2    2    1 
2   3    2    1 
2   4    2    0 
2   5    2    0 
3   1    3    1   W 
3   2    3    0 
3   3    3    0 
3   4    3    0 
3   5    3    0 

然後使用COUNT的最終輸出()函數將這個

member_id consecutives what_to_count 
1   3   W 
2   3   L 
3   1   W 

的what_to_count列確定是否連續的勝利或失敗

ex。

1 - 已經連續3勝

2 - 已經連續3次輸

3 - 已經1勝

請注意,tbl_results是一個表,導致從一個子查詢(其持有通過成員id和日期對原始表進行排序的記錄) - 我只是不在此示例中添加它以使它更短

您可以指出我在查詢中顯示的錯誤在哪裏,甚至儘管我已經知道它是真的有這麼多的錯誤,請原諒我。

您還可以使用正確的MySQL語法重寫我的查詢,或者使用自己的

如果你有更好的想法或任何其他方法除了我已經證明什麼,只要將輸出連勝,並失去, 請告訴我。請。我真的很感激。非常感謝你

+1

id是否可能遵循與match_date不同的順序,這真的是合理嗎? – Strawberry

+0

@Strawberry,我已經可以處理比賽日期順序 - 它存儲在tbl_results_table中,請不要介意比賽日期的順序 – jks

+0

我看不到連續三次贏得member_id = 1。如果按'match_date'排序,member_id 1有W,W,L,W,W –

回答

0

你的問題很混亂。因此,相反,考慮以下...

DROP TABLE IF EXISTS results; 

CREATE TABLE results 
(result_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY 
,member_id INT NOT NULL 
,result CHAR(1) NOT NULL 
); 

INSERT INTO results VALUES 
(1 ,1,'W'), 
(2 ,1,'W'), 
(3 ,1,'W'), 
(4 ,1,'L'), 
(5 ,1,'W'), 
(6 ,2,'L'), 
(7 ,2,'L'), 
(8 ,2,'L'), 
(9 ,2,'W'), 
(10,2,'L'), 
(11,3,'W'), 
(12,3,'L'), 
(13,3,'W'), 
(14,3,'W'), 
(15,3,'W'); 

SELECT a.member_id 
    , a.result_id start 
    , MIN(c.result_id) end 
    , a.result 
    , MIN(c.result_id) - a.result_id +1 total 
    FROM results a 
    LEFT 
    JOIN results b 
    ON b.member_id = a.member_id 
    AND b.result = a.result 
    AND b.result_id + 1 = a.result_id 
    LEFT 
    JOIN results c 
    ON c.member_id = a.member_id 
    AND c.result = a.result 
    AND c.result_id >= a.result_id 
    LEFT 
    JOIN results d 
    ON d.member_id = a.member_id 
    AND d.result = a.result 
    AND d.result_id - 1 = c.result_id 
WHERE b.result_id IS NULL 
    AND c.result_id IS NOT NULL 
    AND d.result_id IS NULL 
GROUP 
    BY a.member_id 
    , a.result_id 
    , a.result; 

+-----------+-------+------+--------+-------+ 
| member_id | start | end | result | total | 
+-----------+-------+------+--------+-------+ 
|   1 |  1 | 3 | W  |  3 | 
|   1 |  4 | 4 | L  |  1 | 
|   1 |  5 | 5 | W  |  1 | 
|   2 |  6 | 8 | L  |  3 | 
|   2 |  9 | 9 | W  |  1 | 
|   2 | 10 | 10 | L  |  1 | 
|   3 | 11 | 11 | W  |  1 | 
|   3 | 12 | 12 | L  |  1 | 
|   3 | 13 | 15 | W  |  3 | 
+-----------+-------+------+--------+-------+ 

此解決方案假定result_id是連續的,但也有解決辦法,如果它不是。隨意挑選那些骨頭。

+0

謝謝你,對不起,我的混淆問題。 – jks

0

我最習慣的SQLite,但這裏有個解決辦法:

-- Create a copy from tbl_result appending a column GR_BEGIN 
-- GR_BEGIN will be 1 when a different result from previous happen 
CREATE TABLE t1 AS SELECT *, COALESCE(prev!=result,1) AS gr_begin FROM (
    SELECT *, (
    SELECT result FROM tbl_results AS x1 WHERE result_id=(
     SELECT MAX(result_id) FROM tbl_results x2 WHERE result_id<tbl_results.result_id AND member_id=tbl_results.member_id 
    ) 
) prev FROM tbl_results 
) AS x3; 

-- Create another table, with another column GR 
-- GR will hold sequence number for consecutive results (for each member) 
CREATE TABLE t2 AS SELECT *, (
    SELECT SUM(gr_begin) FROM t1 x1 WHERE result_id<=t1.result_id AND member_id=t1.member_id 
) gr FROM t1; 

-- Finally, following query will return last result and count of consecutive 
-- results for each member: 
SELECT member_id, result last_result, COUNT(*) consecutive 
FROM t2 
WHERE gr = (SELECT MAX(gr) FROM t2 x1 WHERE member_id=t2.member_id) 
GROUP BY member_id 

檢查它在SQL Fiddle

+0

感謝您的回答 – jks