Chiedo scusa se riapro questo topic, ma mi serve una query adatta per il mio database che consiste nella gestione di una squadra di calcio, compreso di tutto: risultati, classifica etc...
io ho già creato un discreto database, solo che l'aggiornamento della classifica la devo fare io manualmente, infatti se per caso devo correggere un risultato devo cambiare manualmente anche la classifica.
Io invece vorrei fare in modo che cancellando o modificando il risultato di una partita, anche la classifica modifichi in automatico, quindi ho pensato di creare la classifica direttamente attraverso una query, ho provato a costruirla ma purtroppo non mi visualizza nulla.
quindi se qualcuno è disposto a darmi una mano allego tutto il database (sperando che lo capisca) e anche la query che ho provato ad eseguire.
database
--
-- Struttura della tabella `classifica2`
--
CREATE TABLE IF NOT EXISTS `classifica2` (
  `ID_classifica` int(20) NOT NULL AUTO_INCREMENT,
  `squadra` int(20) NOT NULL,
  `punti` int(100) NOT NULL,
  `vinte` int(20) NOT NULL,
  `nulle` int(20) NOT NULL,
  `perse` int(20) NOT NULL,
  `g_fatti` int(200) NOT NULL,
  `g_subiti` int(200) NOT NULL,
  `stagione` int(20) NOT NULL,
  PRIMARY KEY (`ID_classifica`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=55 ;
--
-- Struttura della tabella `giornata`
--
CREATE TABLE IF NOT EXISTS `giornata` (
  `ID_giornata` int(20) NOT NULL AUTO_INCREMENT,
  `numero` int(2) NOT NULL,
  `data_partita` date NOT NULL,
  `stagione` int(20) NOT NULL,
  PRIMARY KEY (`ID_giornata`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=87 ;
--
-- Struttura della tabella `partita`
--
CREATE TABLE IF NOT EXISTS `partita` (
  `ID_partita` int(20) NOT NULL AUTO_INCREMENT,
  `data_partita` date NOT NULL,
  `sq_casa` int(20) NOT NULL,
  `sq_fuori` int(20) NOT NULL,
  `goal_c` int(2) DEFAULT NULL,
  `goal_f` int(2) DEFAULT NULL,
  PRIMARY KEY (`ID_partita`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=720 ;
-- --------------------------------------------------------
--
-- Struttura della tabella `squadra`
--
CREATE TABLE IF NOT EXISTS `squadra` (
  `ID_squadra` int(20) NOT NULL AUTO_INCREMENT,
  `nome` varchar(20) NOT NULL,
  `paese` varchar(20) NOT NULL,
  `stadio` varchar(20) NOT NULL,
  `descrizione` varchar(150) NOT NULL,
  PRIMARY KEY (`ID_squadra`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=37 ;
-- --------------------------------------------------------
--
-- Struttura della tabella `stagione`
--
CREATE TABLE IF NOT EXISTS `stagione` (
  `id_stagione` int(20) NOT NULL AUTO_INCREMENT,
  `anno` varchar(7) NOT NULL,
  `descrizione` varchar(90) NOT NULL,
  PRIMARY KEY (`id_stagione`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
query
SELECT squadra, count(squadra) as partite, 
                sum(if(punteggio=3,1,0)) as vittorie,
                sum(if(punteggio=1,1,0)) as pareggi, 
                sum(if(punteggio=0,1,0)) as sconfitte,
                sum(punteggio) as punteggio, 
                sum(fatti) as fatti, 
                sum(subiti) as subiti
                FROM
                (
                    SELECT sq_casa as squadra, goal_c as fatti, goal_f as subiti, 
                    case
                    when goal_c > goal_f then 3
                    when goal_c = goal_f then 1
                    else 0
                    end as punteggio
                    FROM partita JOIN classifica ON partita.sq_casa = classifica.squadra
                    WHERE classifica.stagione='$stagione' AND data_partita > '2010-09-01'
                    union all 
                    SELECT sq_fuori as squadra, goal_f as fatti,goal_c as subiti,
                    case
                    when goal_f > goal_c then 3
                    when goal_f = goal_c then 1
                    else 0
                    end as punteggio
                    FROM partita JOIN classifica ON partita.sq_fuori = classifica.squadra
                    WHERE classifica.stagione='$stagione'  AND data_partita > '2010-09-01'
                ) as tab                
                ORDER BY punteggio DESC
Sperando in un vostro aiuto
vi ringrazio in anticipo