MySQL序列创建

时间:2015-06-04 01:42:46

标签: mysql

我需要一些帮助来拉动MySQL环境中序列中发生的记录。

我的数据集包括越野游戏和输赢国家。我需要确定连续赢得至少3场比赛的国家。以下是可重复的示例。我创建了一个匹配数据集。

    CREATE TABLE matches (date DATE, winner CHAR(10), loser CHAR(10));
    INSERT INTO matches (date,winner,loser) VALUES (STR_TO_DATE('3-03-2013', '%m-%d-%Y') ,'USA','CHINA');
    INSERT INTO matches (date,winner,loser) VALUES (STR_TO_DATE('3-05-2013', '%m-%d-%Y') ,'USA','RUSSIA');
    INSERT INTO matches (date,winner,loser) VALUES (STR_TO_DATE('3-06-2013', '%m-%d-%Y') ,'FRANCE','GERMANY');
    INSERT INTO matches (date,winner,loser) VALUES (STR_TO_DATE('3-09-2013', '%m-%d-%Y') ,'USA','RUSSIA');
    INSERT INTO matches (date,winner,loser) VALUES (STR_TO_DATE('3-11-2013', '%m-%d-%Y') ,'USA','INDIA');
    INSERT INTO matches (date,winner,loser) VALUES (STR_TO_DATE('3-15-2013', '%m-%d-%Y') ,'USA','AUSTRALIA');
    INSERT INTO matches (date,winner,loser) VALUES (STR_TO_DATE('3-15-2013', '%m-%d-%Y') ,'USA','NEW ZEALAND');

我创建了另一个数据集,其中包含按日期排序的每个国家/地区的行号。

    CREATE TABLE matches2 
    (
         date DATE, 
         winner CHAR(10),
         loser CHAR(10),
         row INT
     );
    INSERT INTO matches2 
    (
        row,
        winner,
        date,
        loser
    )
    SELECT row,
           winner,
           date , 
           loser
    FROM 
    (
       SELECT winner, 
              (@winner:=@winner+1) AS row, 
               date ,
               loser
       FROM matches ,
       (SELECT @winner := 0) r
    ) x
    ORDER BY date;

表match2如下所示

   date         winning   losing        row
   2013-03-03   USA       CHINA          1
   2013-03-05   USA       RUSSIA         2
   2013-03-06   FRANCE    GERMANY        3
   2013-03-09   USA       RUSSIA         4
   2013-03-11   USA       INDIA          5
   2013-03-15   USA       AUSTRALIA      6
   2013-03-15   USA       NEW ZEALAN     7

据数据显示,美国连续赢了3场比赛。我如何编写代码来捕获这个序列?

2 个答案:

答案 0 :(得分:1)

您可以使用一系列连接执行此操作:

select m1.*, m2.date, m3.date
from matches2 m1 join
     matches2 m2
     on m2.row = m1.row + 1 and m2.winner = m1.winner join
     matches2 m3
     on m3.row = m2.row + 1 and m3.winner = m2.winner join
     matches2 m4
     on m4.row = m3.row + 1 and m4.winner = m3.winner;

答案 1 :(得分:0)

这是另一种回归"胜利者"如果我们只考虑该国家作为一个系列参加的比赛,则至少连续三次。也就是说,两个不同国家之间的干预比赛不会被视为打破另一支球队的连胜纪录。

using MSSQLConnector;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1.eyeofheaven
{
    public partial class SearchCustomer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {

            DataSet selectedAngels = connector.ExecuteQuery("select * from customer where idcustomer = 453433");
            DataTable dt = selectedAngels.Tables[0];
        }

    }
}

以下是一个示例测试案例:< / p>

SELECT z.winner
  FROM (SELECT @cnt := IF(v.team=@prev_team AND v.winner=@prev_winner,@cnt+1,1) AS cnt
             , @prev_team   := v.team                                           AS team
             , @prev_winner := v.winner                                         AS winner
          FROM (SELECT t.team
                     , m.winner
                     , m.loser
                     , m.date
                  FROM (SELECT @prev_team := NULL, @prev_winnner := NULL, @cnt := 0) i
                         CROSS
                          JOIN ( SELECT w.winner AS team
                                   FROM matches w
                                  GROUP BY w.winner
                               ) t
                          JOIN matches m
                            ON m.winner = t.team
                     ORDER BY t.team, m.date
               ) v
       ) z
 WHERE z.cnt = 3
 GROUP BY z.winner

在&#39; USA&#39;的比赛中参加过,他们每次都赢了。他们打了5场比赛,他们赢了5场比赛。

法国队还赢得了三场他们参赛的比赛,没有#34;失利&#34;在这些胜利之间。

这个答案中的查询报告了“美国”和“美国”。和&#39; FRANCE&#39;作为赢得连续三个&#34;。