1

PHP/MySQL でスポーツ システムを構築しています。StackOverFlow の助けを借りて、私はそれを構築しました。

私は OW_SPORTS_GAMES テーブルを持っています。これには、チーム ID とスコアでプレイされた各ゲームに関するすべての詳細が含まれています。これが構造です。

ここに画像の説明を入力

また、ゲームの結果に関するユーザーの予測が格納される別のテーブル OW_SPORTS_PREDICTIONS もあります。ユーザーは、どのチームがゲームに勝つかを予測できます。これがテーブル構造です。

ここに画像の説明を入力

各ユーザーが行った正しい予測と間違った予測の数を知ることができる結果を得たいと考えています。推測が正しければ、ユーザーは各ゲームに存在するポイント (ポイント列) を獲得します。出力は、両方のチームのスコアが 0 であるゲームを無視する必要があります。

これが私の現在のSQLです:

SELECT p.userId,
       SUM(IF(g.id IS NOT NULL, 1, 0)) correct,
       SUM(IF(g.id IS NULL, 1, 0)) wrong,
       SUM(IF(g.id IS NOT NULL, g.points, 0)) AS points
  FROM
       (SELECT * FROM ow_sports_games WHERE seasonId = 10 AND (homeTeamScore > 0 OR awayTeamScore > 0) ) g
  RIGHT JOIN ow_sports_predictions p
     ON g.id = p.gameId
    AND p.teamId = IF(g.homeTeamScore > g.awayTeamScore , g.homeTeam, IF(g.homeTeamScore < g.awayTeamScore , g.awayTeam, NULL))
  GROUP BY p.userId ORDER BY points DESC, correct DESC, wrong DESC;

この SQL を使用すると、ユーザーが予測していないゲームとスコア 0-0 も考慮される間違った統計が得られます。

SQL フィドル: http://sqlfiddle.com/#!2/f4c9ed/2

同じフィドル データでは、2 つの正しい予測と 2 つの間違った予測である必要があります。しかし、それは 2 つの正しい予測と 4 つの間違った予測として表示されます。

4

4 に答える 4

1

これが私の答えです。

select sp.userid,
       sum(if ( (sp.teamid = sg.idwinner), 1, 0 )) correct,
       sum(if ( (sp.teamid != sg.idwinner), 0, 1)) wrong,
       sum(if ( (sp.teamid = sg.idwinner), sg.points, 0 )) points
  from (select if ( homeTeamScore>awayTeamScore, homeTeam, awayTeam ) idwinner,
               sg.* 
         from ow_sports_games sg
        where homeTeamScore>0 and awayTeamScore>0 ) sg,
       ow_sports_predictions sp
 where sg.id = sp.gameid
 group by sp.userid

いくつかのポイントを変更して、データをいじります。

于 2013-10-30T19:01:57.960 に答える
0

これはクイックバリアントです。各ユーザーのすべての数値を計算します。もちろん、必要なのは2つだけです。3番目を計算するだけで十分です。

select
  b.userId,
  count(*) as total_predictions,
  sum(if(b.teamId=q.winteam,1,0)) as correct_predictions,
  sum(if(b.teamId<>q.winteam,1,0)) as wrong_predictions
from ow_sports_predictions as b
inner join
(
select
a.id,
if (a.hometeamscore>a.awayteamscore, a.hometeam, if(a.hometeamscore<a.awayteamscore, a.awayteam, -1)) as winteam
from ow_sports_games as a
)
as q
on (b.gameId=q.id)
group by b.userId;

そしてここに簡略化されたバージョンがあります:

select
  b.userId,
  count(*) as total_predictions,
  sum(if(b.teamId=if (q.hometeamscore>q.awayteamscore, q.hometeam, if(q.hometeamscore<q.awayteamscore, q.awayteam, -1)),1,0)) as correct_predictions
from ow_sports_predictions as b
inner join ow_sports_games as q
on (b.gameId=q.id)
group by b.userId;

ポイントを計算するには、次のようにに変更1する必要があります。IFpoints

select
  b.userId,
  count(*) as total_predictions,
  sum(if(b.teamId=if (q.hometeamscore>q.awayteamscore, q.hometeam, if(q.hometeamscore<q.awayteamscore, q.awayteam, -1)),q.points,0)) as correct_predictions_mul_points
from ow_sports_predictions as b
inner join ow_sports_games as q
on (b.gameId=q.id)
group by b.userId;
于 2013-10-30T18:18:14.307 に答える