获得连续输赢的结果

时间:2015-01-09 09:30:31

标签: mysql

我正在努力解决这个问题,我需要获得最新的连续输赢或失去成员的结果

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列确定连续是用于胜利还是用于输掉

离。

1 - 连续3次获胜

2 - 连续3次失败

3 - 获胜1次

请注意,tbl_results是一个由子查询产生的表(它包含按成员ID和日期对原始表进行排序的记录) - 我只是不在此示例中添加它以使其更短

你可以指出我所显示的查询中的错误在哪里,即使我已经知道它确实有很多错误,请原谅我。

您也可以使用正确的mysql语法重写我的查询或使用您自己的

如果你有更好的想法或任何其他方法,只要它会输出连续输赢,请告诉我。请。我真的很感激。非常感谢你

2 个答案:

答案 0 :(得分: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是连续的,但如果不是,则会有解决方法。随意挑选骨头。

答案 1 :(得分: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中查看!