0

I have such a database, stocking opponents, matches, odds and bets from users.

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);

For this thread, I shortened data and fields to only relevant informations.

First, I query the list of matches and then I loop on them and push 'opponents' cell into each match, and each opponent contains the count of bets for this match, with the following query :

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`

What I'd like to add is the count of total bets for this match, into the opponents data. So into each match, each opponents would contains his own total votes, and the total votes on the match (which is equal to the total votes of the opponent + the total votes of the other team). I could do that in PHP, but i'd like to get this information directly from my SQL query. I tried something like this :

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`

But for both teams on match #1, total_votes is equal to 4, instead of 2.

Desired output would be an array like this :

["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"
      }
}

Any help would be appreciated to add this "match_votes" cell.

4

0 回答 0