Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Calcolo in php+sql

  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2011
    Messaggi
    2

    Calcolo in php+sql

    Salve a tutti sono nuovo del forum...volevo chiedervi come faccio a fare la somma di tutti i campi importo_entrata dalla tabella entrate e sottrarlo alla somma di tutti i campi importo_uscita dalla tabella uscite
    ho provato con la seguente query:

    $query1 = "SELECT sum(importo_entrata)-sum(importo_uscita) as fondo_cassa FROM entrate, uscite";

    ma mi restituisce un risultato sballato
    questo è tutto il codice della pagina php:

    -------------------------------------------------------------------------------

    <?php

    include("config.inc.php");

    $query1 = "SELECT sum(importo_entrata)-sum(importo_uscita) as fondo_cassa FROM entrate, uscite";

    $query2 = "SELECT * FROM entrate";
    $query3 = "SELECT * FROM uscite";

    // Richiesta
    $exquery1 = mysql_query($query1,$db) or die("Errore nell'estrazione dei dati1");
    $exquery2 = mysql_query($query2,$db) or die("Errore nell'estrazione dei dati2");
    $exquery3 = mysql_query($query3,$db) or die("Errore nell'estrazione dei dati3");


    // Esecuzione della query2

    echo "

    <table border=2>
    <tr>
    <td width=130> Entrata </td>
    <td width=80> Data Entrata </td>
    <td width=150> Motivazione </td>
    </tr>
    </table>";
    while($row = mysql_fetch_array($exquery2)){

    echo "<table border=2>
    <tr>
    <td width=130> $row[importo_entrata] </td>
    <td width=80> $row[data_entrata] </td>
    <td width=150> $row[motivazione_entrata] </td>
    </tr>
    </table>";
    }
    // Esecuzione della query
    echo "
    <table border=2>
    <tr>
    <td width=130> Uscita </td>
    <td width=80> Data Uscita </td>
    <td width=150> Motivazione</td>
    </tr>
    </table>";
    while($row = mysql_fetch_array($exquery3)){

    echo "<table border=2>
    <tr>
    <td width=130> $row[importo_uscita] </td>
    <td width=80> $row[data_uscita] </td>
    <td width=150> $row[motivazione_uscita] </td>
    </tr>
    </table>";
    }
    // Esecuzione della query1
    echo "
    <table border=2>
    <tr>
    <td width=130> Fondo Cassa </td>
    <td width=80> Data </td>

    </tr>
    </table>";
    while($row = mysql_fetch_array($exquery1)){

    echo "<table border=2>
    <tr>
    <td width=130> $row[fondo_cassa] </td>
    </tr>
    </table>";
    }
    mysql_close($db);
    echo "<a href=\"Movimenti.html\">Torna ai movimenti</a>";
    ?>

  2. #2
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,922
    codice:
    select t1.te-t2.tu as fondo_cassa from(
       (select sum(importo_entrata) as te from entrate) as t1,
       (select sum(importo_uscita) as tu from uscite) as t2
    )

  3. #3
    Utente di HTML.it
    Registrato dal
    Apr 2011
    Messaggi
    2
    Grazie adesso funziona benissimo

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.