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".