1

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?

2 Answers 2

2
$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.

1

In a single SQL:

SELECT
    ( SELECT COUNT(*) FROM ... ) -
    ( SELECT COUNT(*) FROM ... );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.