简体   繁体   English

每支球队每周只获得一个“足球对决”结果

[英]Getting only one result for a “football matchup” per team per week

I'm putting together a new project, and one of the things I need to do is get the results of a Fake Football (American) match.我正在整理一个新项目,我需要做的一件事是获得假足球(美国)比赛的结果。 I'm doing this in php and mysql, utilizing CodeIgniter as my framework.我在 php 和 mysql 中执行此操作,使用 CodeIgniter 作为我的框架。 My dbfiddle is as follows:我的dbfiddle如下:

https://www.db-fiddle.com/f/5Q7QezddpNEGabaia2w5FQ/0 https://www.db-fiddle.com/f/5Q7QezddpNEGabaia2w5FQ/0

Now, as you can see, each team has an entry for home_team_id and away_team_id.现在,如您所见,每支球队都有一个 home_team_id 和 away_team_id 条目。 What I would like to do is have a query that gets only ONE result for each team (regardless if they are home or away).我想做的是有一个查询,每个团队只获得一个结果(无论他们是在家还是在客场)。 I'd rather not programmatically process this data if I don't have to.如果不需要,我宁愿不以编程方式处理这些数据。

The results should be something along the lines of the following:结果应该类似于以下内容:

SELECT week, home_team_id, away_team_id, 
    my_score, their_score, 
    a.team_name as home_team, b.team_name as away_team 
FROM nfl_user_matchups nm 
LEFT JOIN user_teams a ON nm.home_team_id = a.user_teams_id 
LEFT JOIN user_teams b ON nm.away_team_id = b.user_teams_id 
WHERE week = 1

Expected Final Data would be something like this (my_score would be home_team_id score, their_score would be away_team_id score):预期的最终数据将是这样的(my_score 将是 home_team_id 得分,他们的_score 将是 away_team_id 得分):

+------+--------------+--------------+----------+-------------+
| week | home_team_id | away_team_id | my_score | their_score |
+------+--------------+--------------+----------+-------------+
| 1    | 3            | 9            | 112      | 144         |
+------+--------------+--------------+----------+-------------+
| 1    | 7            | 2            | 85       | 96          |
+------+--------------+--------------+----------+-------------+
| 1    | 1            | 6            | 111      | 114         |
+------+--------------+--------------+----------+-------------+
| 1    | 4            | 5            | 99       | 125         |
+------+--------------+--------------+----------+-------------+
| 1    | 8            | 10           | 140      | 122         |
+------+--------------+--------------+----------+-------------+

Your problem statement basically means that a pair of teams competing, viz.您的问题陈述基本上意味着一对团队竞争,即。 , (1,2) are to be treated same as (2,1) . , (1,2)将被视为与(2,1)相同。 One way to handle such requirements is to ensure that for any match between these two teams, we ensure that they are in the same order.处理此类要求的一种方法是确保对于这两个团队之间的任何比赛,我们确保它们的顺序相同。 So, basically we get the Least() team id value out of the two teams, and always put it in the first index;所以,基本上我们从两个团队中得到Least()团队 id 值,并且总是把它放在第一个索引中; and the Greatest() value always at the second (last) index.并且Greatest()值始终位于第二个(最后一个)索引处。 It is noteworthy that Greatest() is not same as Max() .值得注意的是Greatest()Max()不同。 Max() function computes maximum on a column; Max() function 计算列的最大值; while Greatest() is generally used to compare values across a row.Greatest()通常用于比较一行中的值。

Now, we can simply GROUP BY on this pair and compute the aggregates as desired.现在,我们可以简单地在这对上进行GROUP BY并根据需要计算聚合。 While determining home/away scores, you will need to use CASE.. WHEN expression to determine whether a particular row has home id the least, or away id:在确定主场/客场得分时,您将需要使用CASE.. WHEN表达式来确定特定行的主场 ID 最少还是客场 ID:

SELECT 
  week, 
  LEAST(home_team_id, away_team_id) AS home_id, 
  GREATEST(home_team_id, away_team_id) AS away_id, 
  MAX(CASE 
      WHEN LEAST(home_team_id, away_team_id) = home_team_id 
      THEN my_score 
      ELSE their_score
      END) AS home_score,
  MAX(CASE 
      WHEN GREATEST(home_team_id, away_team_id) = away_team_id 
      THEN their_score 
      ELSE my_score
      END) AS away_score 
FROM nfl_user_matchups 
WHERE week = 1
GROUP BY
  week, 
  home_id, 
  away_id;

View on DB Fiddle在 DB Fiddle 上查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM