Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Pemesanan Dinamis liga sepak bola yang rumit di MySQL?

Mari kita lakukan langkah demi langkah:

Pilih pertandingan yang dimenangkan di kandang dan skor di kandang:

   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

Sebut saja hasil ini HOME_GAMES.

Pilih pertandingan yang dimenangkan dan skor pertandingan tandang:

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

Sebut saja hasil ini AWAY_GAMES.

Pilih total permainan yang dimenangkan dan skor totalnya:

   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

==> Gabungkan semuanya dengan mengganti AWAY_GAMES dan HOME_GAMES:

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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Tidak ingin mysql mentransmisikan String ke Integer secara otomatis

  2. Menghubungkan Aplikasi Heroku Lumen ke Amazon RDS MySQL 5.7.19

  3. Mencari susunan MySQL yang tidak peka huruf besar-kecil di mana a !=

  4. Bagaimana cara memesan kueri MySQL dengan kolom tertentu?

  5. PDO Dapatkan Beberapa ID Sisipan