sql >> Database >  >> RDS >> Mysql

Ingewikkelde voetbalcompetitie Dynamisch bestellen in MySQL?

Laten we het stap voor stap doen:

Selecteer de gewonnen wedstrijden thuis en de score thuis:

   SELECT COUNT(*) as wins, SUM(G.home_score) as score FROM games G WHERE 
      G.team_id = T.team_id #See 3. query and you'll understand
      G.home_score > away_score

Laten we dit resultaat HOME_GAMES noemen.

Selecteer de gewonnen wedstrijden en de score van uitwedstrijden:

SELECT COUNT(*) as wins, SUM(G.away_score) as score FROM games G
WHERE 
  G.team_id = T.team_id #See 3. query and you'll understand
  G.away_score > G.home_score

Laten we dit resultaat AWAY_GAMES noemen.

Selecteer het totaal aantal gewonnen games en de totale score:

   SELECT (A.wins + H.wins) AS total_wins, (A.score + H.score) AS total_score FROM
   (AWAY_GAMES) AS A, (HOME_GAMES) AS H, teams T 
   ORDER BY total_wins, total_score

==> Voeg alles samen door AWAY_GAMES en HOME_GAMES te vervangen:

SELECT (A.wins + H.wins) AS total_wins, (A.score + H.score) AS total_score FROM 
  (SELECT COUNT(*) as wins, SUM(G.away_score) as score FROM games G
   WHERE 
     G.team_id = T.team_id #See 3. and you'll understand
     G.away_score > G.home_score) AS A, 

   (SELECT COUNT(*) as wins, SUM(G.home_score) as score FROM games G 
   WHERE 
      G.team_id = T.team_id #See 3. and you'll understand
      G.home_score > away_score) AS H, 

   teams T
   ORDER BY total_wins, total_score 


  1. Afstemmen van SQL-statements in SQL Developer

  2. Hoe CHAR() werkt in MariaDB

  3. Hoe een kolomnaam in SQL te hernoemen?

  4. WordPress pre_get_posts werken niet