sql >> Database teknologi >  >> RDS >> Mysql

sql til beregning af point for spil

Du kan gøre det ved at bruge UNION :

SELECT team, SUM(points) AS total_points
FROM (
  SELECT team_home AS team, 
         CASE 
            WHEN team_home_goals > team_away_goals THEN 3
            WHEN team_home_goals = team_away_goals THEN 1
            ELSE 0
         END AS points
  FROM games

  UNION ALL

  SELECT team_away AS team, 
         CASE 
            WHEN team_away_goals > team_home_goals THEN 3
            WHEN team_away_goals = team_home_goals THEN 1
            ELSE 0
         END AS points
  FROM games ) AS t
GROUP BY team
ORDER BY total_points DESC

Demo her




  1. Dvale respekterer ikke MySQL auto_increment primære nøglefelt

  2. SQLSTATE[HY000] [1040] For mange forbindelser

  3. MYSQL - Sådan omgås rækkestørrelsesgrænsen på 66 KBytes

  4. Databasedesign til flersprogede applikationer