我目前這個問題所困擾,我需要獲得最新的連續輸贏成員的結果得到連續輸多贏結果
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語法重寫我的查詢,或者使用自己的
如果你有更好的想法或任何其他方法除了我已經證明什麼,只要將輸出連勝,並失去, 請告訴我。請。我真的很感激。非常感謝你
id是否可能遵循與match_date不同的順序,這真的是合理嗎? – Strawberry
@Strawberry,我已經可以處理比賽日期順序 - 它存儲在tbl_results_table中,請不要介意比賽日期的順序 – jks
我看不到連續三次贏得member_id = 1。如果按'match_date'排序,member_id 1有W,W,L,W,W –