在整个数据列表中计算一行

时间:2016-02-07 22:51:00

标签: mysql

我有一张表格,其中包含特定比赛的比赛。

当我选择MatchIDAuto 4050时,我需要知道这是本场比赛共29场比赛的第15场比赛。

我显然可以确定29场比赛只有来自Match2的简单选择计数(*),其中CompetitionIDAuto = 669

有没有人知道如何确定而不需要循环,例如4050是匹配15?

    --
    -- Table structure for table `Match2`
    --

    CREATE TABLE IF NOT EXISTS `Match2` (
    `MatchIDAuto` int(10) NOT NULL AUTO_INCREMENT,
    `CompetitionIDAuto` int(10) NOT NULL DEFAULT '0',
    `TeamHome` varchar(100) NOT NULL DEFAULT '0',
    `TeamAway` varchar(100) NOT NULL DEFAULT '0',
    `MatchDate` date NOT NULL DEFAULT '0000-00-00',
    `MatchTime` time NOT NULL DEFAULT '00:00:00',
    `MatchStartTime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
    `VenueIDAuto` int(10) NOT NULL DEFAULT '0',
    `GameEnded` int(1) NOT NULL DEFAULT '0',
    `PredictionsGameEnded` int(1) NOT NULL DEFAULT '0',
    `Result` varchar(50) NOT NULL DEFAULT '',
    `ResultURL` varchar(100) NOT NULL DEFAULT '',
    `DateCreated` date NOT NULL DEFAULT '0000-00-00',
    `DateCreatedTimeStamp` int(10) NOT NULL DEFAULT '0',
    `DateModified` date NOT NULL DEFAULT '0000-00-00',
    `UserCreated` varchar(50) NOT NULL DEFAULT '',
    `DisableThisMatch` int(1) NOT NULL DEFAULT '0',
    `AllocatedTo` varchar(25) NOT NULL,
    `MatchUpdated` tinyint(1) NOT NULL DEFAULT '0',
    PRIMARY KEY (`MatchIDAuto`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4234 ;

    --
    -- Dumping data for table `Match2`
    --

   INSERT INTO `Match2` (`MatchIDAuto`, `CompetitionIDAuto`, `TeamHome`, `TeamAway`, `MatchDate`, `MatchTime`, `MatchStartTime`, `VenueIDAuto`, `GameEnded`, `PredictionsGameEnded`, `Result`, `ResultURL`, `DateCreated`, `DateCreatedTimeStamp`, `DateModified`, `UserCreated`, `DisableThisMatch`, `AllocatedTo`, `MatchUpdated`) VALUES
   (4036, 669, 'Brisbane Heat Women', 'Hobart Hurricanes Women', '2016-01-01', '03:40:00', '2015-12-31 21:40:00', 7, 0, 0, '', '', '2015-11-13', 1447473564, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4037, 669, 'Perth Scorchers Women', 'Adelaide Strikers Women', '2016-01-01', '04:10:00', '2015-12-31 22:10:00', 3, 0, 0, '', '', '2015-11-13', 1447474751, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4038, 669, 'Sydney Thunder Women', 'Brisbane Heat Women', '2016-01-02', '01:00:00', '2016-01-01 19:00:00', 316, 0, 0, '', '', '2015-11-13', 1447474966, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4039, 669, 'Melbourne Stars Women', 'Melbourne Renegades Women', '2016-01-02', '02:40:00', '2016-01-01 20:40:00', 8, 0, 0, '', '', '2015-11-13', 1447475064, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4040, 669, 'Sydney Thunder Women', 'Hobart Hurricanes Women', '2016-01-02', '07:00:00', '2016-01-02 01:00:00', 7, 0, 0, '', '', '2015-11-13', 1447475178, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4041, 669, 'Hobart Hurricanes Women', 'Brisbane Heat Women', '2016-01-03', '00:00:01', '2016-01-02 18:00:01', 316, 0, 0, '', '', '2015-11-13', 1447475258, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4042, 669, 'Melbourne Renegades Women', 'Melbourne Stars Women', '2016-01-03', '03:30:00', '2016-01-02 21:30:00', 8, 0, 0, '', '', '2015-11-13', 1447475350, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4043, 669, 'Hobart Hurricanes Women', 'Sydney Thunder Women', '2016-01-03', '04:00:00', '2016-01-02 22:00:00', 316, 0, 0, '', '', '2015-11-13', 1447475429, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4044, 669, 'Adelaide Strikers Women', 'Melbourne Stars Women', '2016-01-07', '23:00:00', '2016-01-07 17:00:00', 310, 0, 0, '', '', '2015-11-13', 1447475525, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4045, 669, 'Melbourne Renegades Women', 'Sydney Sixers Women', '2016-01-08', '03:30:00', '2016-01-07 21:30:00', 310, 0, 0, '', '', '2015-11-13', 1447475613, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4046, 669, 'Sydney Sixers Women', 'Adelaide Strikers Women', '2016-01-08', '23:00:00', '2016-01-08 17:00:00', 310, 0, 0, '', '', '2015-11-13', 1447475711, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4047, 669, 'Adelaide Strikers Women', 'Sydney Sixers Women', '2016-01-09', '03:30:00', '2016-01-08 21:30:00', 310, 0, 0, '', '', '2015-11-13', 1447475802, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4048, 669, 'Melbourne Renegades Women', 'Sydney Thunder Women', '2016-01-09', '03:40:00', '2016-01-08 21:40:00', 317, 0, 0, '', '', '2015-11-13', 1447476726, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4049, 669, 'Melbourne Stars Women', 'Adelaide Strikers Women', '2016-01-09', '23:00:00', '2016-01-09 17:00:00', 310, 0, 0, '', '', '2015-11-13', 1447476819, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4050, 669, 'Sydney Sixers Women', 'Melbourne Renegades Women', '2016-01-10', '03:30:00', '2016-01-09 21:30:00', 310, 0, 0, '', '', '2015-11-13', 1447476917, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4051, 669, 'Melbourne Stars Women', 'Sydney Thunder Women', '2016-01-15', '03:30:00', '2016-01-14 21:30:00', 318, 0, 0, '', '', '2015-11-13', 1447477125, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4052, 669, 'Hobart Hurricanes Women', 'Sydney Sixers Women', '2016-01-15', '03:30:00', '2016-01-14 21:30:00', 319, 0, 0, '', '', '2015-11-13', 1447477325, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4053, 669, 'Adelaide Strikers Women', 'Melbourne Renegades Women', '2016-01-15', '03:30:00', '2016-01-14 21:30:00', 320, 0, 0, '', '', '2015-11-13', 1447477775, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4054, 669, 'Adelaide Strikers Women', 'Brisbane Heat Women', '2016-01-15', '23:30:00', '2016-01-15 17:30:00', 320, 0, 0, '', '', '2015-11-13', 1447477920, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4055, 669, 'Sydney Sixers Women', 'Sydney Thunder Women', '2016-01-16', '02:40:00', '2016-01-15 20:40:00', 10, 0, 0, '', '', '2015-11-13', 1447478942, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4056, 669, 'Hobart Hurricanes Women', 'Melbourne Stars Women', '2016-01-16', '03:30:00', '2016-01-15 21:30:00', 315, 0, 0, '', '', '2015-11-13', 1447479213, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4057, 669, 'Perth Scorchers Women', 'Melbourne Renegades Women', '2016-01-16', '04:10:00', '2016-01-15 22:10:00', 320, 0, 0, '', '', '2015-11-13', 1447479324, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4058, 669, 'Sydney Thunder Women', 'Melbourne Stars Women', '2016-01-16', '23:00:00', '2016-01-16 17:00:00', 321, 0, 0, '', '', '2015-11-13', 1447479527, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4059, 669, 'Sydney Sixers Women', 'Hobart Hurricanes Women', '2016-01-16', '23:00:00', '2016-01-16 17:00:00', 322, 0, 0, '', '', '2015-11-13', 1447479733, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4060, 669, 'Melbourne Renegades Women', 'Adelaide Strikers Women', '2016-01-16', '23:30:00', '2016-01-16 17:30:00', 320, 0, 0, '', '', '2015-11-13', 1447479830, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4061, 669, 'Melbourne Renegades Women', 'Perth Scorchers Women', '2016-01-17', '04:10:00', '2016-01-16 22:10:00', 320, 0, 0, '', '', '2015-11-13', 1447479954, '2015-11-13', 'Jerry', 0, 'Jerry', 0),
   (4064, 669, 'Semi Final', 'Semi Final', '2016-01-21', '00:00:01', '2016-01-20 18:00:01', 323, 0, 0, '', '', '2015-11-14', 1447523483, '2015-11-14', 'Jerry', 0, 'Jerry', 0),
   (4065, 669, 'Semi Final', 'Semi Final', '2016-01-22', '00:00:01', '2016-01-21 18:00:01', 323, 0, 0, '', '', '2015-11-14', 1447523558, '2015-11-14', 'Jerry', 0, 'Jerry', 0),
   (4066, 669, 'Final', 'Final', '2016-01-24', '00:00:01', '2016-01-23 18:00:01', 323, 0, 0, '', '', '2015-11-14', 1447523614, '2015-11-14', 'Jerry', 0, 'Jerry', 0);

1 个答案:

答案 0 :(得分:0)

我假设你的比赛总是以升序(按日期)插入,这样第一场比赛就有最低的MatchIDAuto。

所以试试这个

SELECT m.MatchIDAuto, COUNT( * ) AS row_number
FROM Match2 m
JOIN Match2 m2 ON m.MatchIDAuto >= m2.MatchIDAuto
WHERE m.MatchIDAuto = 4050
GROUP BY m.MatchIDAuto

输出

+-------------+------------+
| MatchIDAuto | row_number |
+-------------+------------+
|        4050 |         15 |
+-------------+------------+