Mysql,计数两个对手的投票,+两个人的总和

时间:2013-11-12 11:49:51

标签: mysql count totals

我有这样一个数据库,存储来自用户的对手,比赛,赔率和投注。

CREATE TABLE IF NOT EXISTS `opponents` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;

INSERT INTO `opponents` (`id`, `name`) VALUES
(1, 'Team 1'),
(8, 'Team 2');

CREATE TABLE IF NOT EXISTS `matches` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `statut` tinyint(4) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `game_id` (`game_id`),
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=29 ;

INSERT INTO `matches` (`id`, `statut`) VALUES
(1, 1);

CREATE TABLE IF NOT EXISTS `odds` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `match_id` int(11) NOT NULL,
  `opponent_id` int(11) NOT NULL,
  `value` float NOT NULL,
  PRIMARY KEY (`id`),
  KEY `match_id` (`match_id`),
  KEY `opponent_id` (`opponent_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=63 ;


INSERT INTO `odds` (`id`, `datetime`, `match_id`, `opponent_id`, `value`) VALUES
(1, '2013-11-05 16:33:26', 1, 1, 1),
(2, '2013-11-05 16:33:26', 1, 8, 1);

CREATE TABLE IF NOT EXISTS `bets` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `user_id` int(11) NOT NULL,
  `match_id` int(11) NOT NULL,
  `opponent_id` int(11) NOT NULL,
  `odds_id` int(11) NOT NULL,
  `statut` tinyint(4) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `match_id` (`match_id`),
  KEY `opponent_id` (`opponent_id`),
  KEY `user_id` (`user_id`),
  KEY `odds_id` (`odds_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;

INSERT INTO `bets` (`id`, `datetime`, `user_id`, `match_id`, `opponent_id`, `odds_id`, `statut`) VALUES
(1, '2013-11-05 17:24:08', 2, 1, 8, 0, 1),
(3, '2013-11-06 11:21:19', 25, 1, 8, 0, 1);

对于这个帖子,我将数据和字段缩短为只有相关的信息。

首先,我查询匹配列表,然后循环它们并将'对手'单元格推入每个匹配,并且每个对手包含此匹配的投注计数,并使用以下查询:

SELECT `o`.`id` AS odds_id, `o`.`datetime`, `o`.`opponent_id`, `op`.`name` COUNT(b.id) AS votes FROM (`odds` o) INNER JOIN `opponents` op ON `o`.`opponent_id` = `op`.`id` LEFT JOIN `bets` b ON `b`.`opponent_id` = `o`.`opponent_id` AND b.match_id = o.match_id WHERE `o`.`match_id` = '1' GROUP BY `op`.`id`

我想补充的是这场比赛的总赌注数,进入对手数据。因此,在每场比赛中,每个对手将包含他自己的总票数和比赛的总票数(等于对手的总票数+其他团队的总票数)。我可以在PHP中做到这一点,但我想直接从我的SQL查询中获取此信息。 我试过这样的事情:

SELECT `o`.`id` AS odds_id, `o`.`datetime`, `o`.`opponent_id`, `op`.`name` COUNT(b.id) AS votes FROM (`odds` o), COUNT(b2.id) AS match_votes FROM (`odds` o) INNER JOIN `opponents` op ON `o`.`opponent_id` = `op`.`id` LEFT JOIN `bets` b ON `b`.`opponent_id` = `o`.`opponent_id` AND b.match_id = o.match_id LEFT JOIN `bets` b2 ON b2.match_id = o.match_id WHERE `o`.`match_id` = '1' GROUP BY `op`.`id`

但对于比赛#1的两支球队,total_votes等于4,而不是2。

所需的输出将是这样的数组:

["opponents"]=>
    array(2) {
      [0]=>
      array(7) {
        ["odds_id"]=>
        string(1) "2"
        ["datetime"]=>
        string(19) "2013-11-05 17:33:26"
        ["opponent_id"]=>
        string(1) "1"
        ["name"]=>
        string(5) "Team #1"
        ["votes"]=>
        string(1) "0"
        ["match_votes"]=>
        string(1) "2"
      }
      [1]=>
      array(7) {
        ["odds_id"]=>
        string(1) "1"
        ["datetime"]=>
        string(19) "2013-11-05 17:33:26"
        ["opponent_id"]=>
        string(1) "8"
        ["name"]=>
        string(8) "Team #2"
        ["votes"]=>
        string(1) "2"
        ["match_votes"]=>
        string(1) "2"
      }
}

任何帮助都会被赞成添加这个“match_votes”单元格。

0 个答案:

没有答案