Mysql – How to subtract an object from another in PHP/MySQL

MySQLPHP

I am printing totalbooks with this code:

$query ="select sum(copies) as totalbooks from booksdetail";
$result=mysqli_query($con, $query);
while($row=mysqli_fetch_array($result)){
echo $row['totalbooks'];     

and printing issued books with this code:

$query ="select count(id) as issuedbooks from issue";
$result=mysqli_query($con, $query);
while($row=mysqli_fetch_array($result)){
echo $row['issuedbooks'];     

Now, I want to subtract issuedbooks from totalbooks.

Which code I should use?

Best Answer

$query ="select sum(copies) as totalbooks from booksdetail";
$result=mysqli_query($con, $query);
while($row=mysqli_fetch_array($result)){
$totalbooks = $row['totalbooks'];  

$query ="select count(id) as issuedbooks from issue";
$result=mysqli_query($con, $query);
while($row=mysqli_fetch_array($result)){
$issuedbooks = $row['issuedbooks'];   

$remainingbooks = $totalbooks - $issuedbooks;

PHP is off-topic for this site though, really.