Buongiorno a tutti.
Ho una tabella fattura così composta:
CREATE TABLE IF NOT EXISTS `fattura` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`numfattura` int(5) NOT NULL COMMENT 'numero_fattura',
`fornitore` smallint(3) NOT NULL COMMENT 'fornitore',
`data` date NOT NULL COMMENT 'data fattura',
`pagamento` int(11) DEFAULT NULL COMMENT 'metodo_pagamento',
`nassriba` varchar(50) DEFAULT NULL,
`imponibile` double NOT NULL,
`iva` double NOT NULL,
`totale` double NOT NULL,
`tipologia` int(11) NOT NULL,
`controllo` int(11) NOT NULL COMMENT 'campo_controllo',
UNIQUE KEY `ID` (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
ed una tabella righefattura:
CREATE TABLE IF NOT EXISTS `righefattura` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`numfattura` bigint(5) NOT NULL,
`fornitore` smallint(3) NOT NULL,
`modello` int(11) NOT NULL,
`tipomodello` int(11) NOT NULL,
`quantita` int(5) NOT NULL,
`prezzo` double NOT NULL,
`codarticolo` varchar(50) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=73 ;
La relazione tra le due è uno a molti. Vorrei poter avere come risultato una riga per ogni fattura dove viene visualizzato l'imponibile della fattura ed il totale delle righefattura per controllare che imponibile e somma righe siano coincidenti.
Ho provato a creare una query di questo tipo:
CREATE TEMPORARY TABLE temp SELECT numfattura,fornitore,sum(prezzo) as prezzototale FROM `righefattura`
GROUP BY numfattura, fornitore
select numfattura.fattura, fornitore.fattura
from fattura
left join temp ON temp.numfattura = fattura.numfattura
Ma ho errore di sintassi.
Vi chiedo: come posso risolvere?
Grazie.

Rispondi quotando