0

私はそのようなデータベースを持っており、ユーザーからの対戦相手、試合、オッズ、賭けをストックしています。

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 は 2 ではなく 4 です。

望ましい出力は、次のような配列になります。

["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」セルを追加していただければ幸いです。

4

0 に答える 0