Pregunta

Hi i have two table in my database 1. Income 2. Outcome Income table has ID, Date, Price also Outcome table exactly same im showing Sum of prices for today like that

mysql_select_db($database_conn, $conn);
$query_gelen_gun = sprintf ("SELECT SUM(tutar) FROM cepbank WHERE tarih >= DATE(NOW())");
$gelen_gun = mysql_query($query_gelen_gun, $conn) or die(mysql_error());
$row_gelen_gun = mysql_fetch_assoc($gelen_gun);
$totalRows_gelen_gun = mysql_num_rows($gelen_gun);

mysql_select_db($database_conn, $conn);
$query_giden_gun = sprintf ("SELECT SUM(tutar) FROM cepbank_out WHERE tarih >= DATE(NOW())");
$giden_gun = mysql_query($query_giden_gun, $conn) or die(mysql_error());
$row_giden_gun = mysql_fetch_assoc($giden_gun);
$totalRows_giden_gun = mysql_num_rows($giden_gun);

. . .

<p>Total Income for Today:<?php echo $row_gelen_gun['SUM(tutar)']; ?></p>
<p>Total Outcome for Today:<?php echo $row_giden_gun['SUM(tutar)']; ?></p>

Now how i can show balance of this two result for example $row_gelen_gun['SUM(tutar)'] = 100 and $row_giden_gun['SUM(tutar)'] = 50

balance should be 100-50 =50 ?

Note: Price=tutar in query

¿Fue útil?

Solución

You either want to use union all or cross join. Use union all to get the results in two rows. Use cross join to get the results in two columns on the same row.

Here is an example for the cross join:

select sumcep, sumcepout
from (SELECT SUM(tutar) as sumcep
      FROM cepbank
      WHERE tarih >= DATE(NOW())
     ) t cross join
     (SELECT SUM(tutar) as sumcepout
      FROM cepbank_out
      WHERE tarih >= DATE(NOW())
     ) t2

Otros consejos

You can do it with Gordon's query, and if you want MySQL to calculate the balance just change the first line to this:

select sumcep, sumcepout, sumcep-sumcepout as balance

If you want to stay with your current approach (two separate queries), here are some recommendations:

First, I'd alias the columns because having them both named SUM(tutar) can be confusing. Also, I've always been uneasy about trusting the generated column name when a function is involved. So instead of these:

SELECT SUM(tutar) FROM cepbank WHERE tarih >= DATE(NOW())
SELECT SUM(tutar) FROM cepbank_out WHERE tarih >= DATE(NOW())

... try these:

SELECT SUM(tutar) AS Income FROM cepbank WHERE tarih >= DATE(NOW())
SELECT SUM(tutar) AS Outcome FROM cepbank_out WHERE tarih >= DATE(NOW())

And then for balance you can do this:

$balance = $row_gelen_gun['Income'] - $row_giden_gun['Outcome'];

PHP will cast/convert to number for you.

BTW, you can use CURRENT_DATE instead of DATE(NOW()), but that's minor. Change if you think it makes the query more readable - your call :)

But most importantly, if this is anything more than a school assignment or a learning exercise, please please use PDO or MySQLi. Even if it is a learning exercise, try getting it to work as is and then converting to PDO or MySQLi - you'll need to use them in the "real world".

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top