0
I think you don’t need this other table. Maybe a view can be useful, depends on your decision.
Try the SQL below:
SELECT
CUSTAS_SOMA,
RECEITAS_SOMA,
(RECEITAS_SOMA-CUSTAS_SOMA) AS SALDO
FROM (
SELECT
(SELECT SUM(valor_custas) FROM custas ) AS CUSTAS_SOMA,
(SELECT SUM(valor_receitas) FROM receitas) AS RECEITAS_SOMA
) AS SOMAS
In codeigniter, you can use:
$result=$this->db->query("
SELECT
CUSTAS_SOMA,
RECEITAS_SOMA,
(RECEITAS_SOMA-CUSTAS_SOMA) AS SALDO
FROM (
SELECT
(SELECT SUM(valor_custas) FROM custas ) AS CUSTAS_SOMA,
(SELECT SUM(valor_receitas) FROM receitas) AS RECEITAS_SOMA
) AS SOMAS
"));
$S=$result->first_row();
echo "Custos: ".$S->CUSTAS_SOMA."<br />";
echo "Receitas: ".$S->RECEITAS_SOMA."<br />";
echo "Saldo: ".$S->SALDO;
the fields I need to make a sum are: recipe value and cost_value. All float type
– Ramiro
@Virgilionovic I actually created a chart just for that.
– Ramiro
I created a table called saldoprocesso to do the joins, but I doubt if this is necessary? and if it works by doing this too.
– Ramiro