我更熟悉php并制作图书馆管理项目。我想在图书馆中打印全书,因为我使用了以下代码:
<?php
$query ="select sum(copies) as totalbooks from booksdetail";
$result=mysqli_query($con, $query);
while($row=mysqli_fetch_array($result)){
echo $row['totalbooks'];
}
?>
我想打印已发行的书籍总数,因为我使用了以下代码:
<?php
$query ="select count(id) as issuedbooks from issue";
$result=mysqli_query($con, $query);
while($row=mysqli_fetch_array($result)){
echo $row['issuedbooks'];
}
?>
现在,我想要打印库中可用的书籍数量,因为我必须从issued books
中减去total books
。
请建议我应该使用哪个代码进行减法?
答案 0 :(得分:0)
<?php
$totalBooks=0;
$query ="select sum(copies) as totalbooks from booksdetail";
$result=mysqli_query($con, $query);
while($row=mysqli_fetch_array($result)){
$totalBooks=$row['totalbooks'];
}?>
<?php
$issuedBooks=0;
$query ="select count(id) as issuedbooks from issue";
$result=mysqli_query($con, $query);
while($row=mysqli_fetch_array($result)){
$issuedBooks=$row['issuedbooks'];
}?>
<?
echo $availableBooks = $totalBooks - $issuedBooks;
?>
答案 1 :(得分:0)
就是这样:
<?php
$query ="select sum(copies) as totalbooks from booksdetail";
$result=mysqli_query($con, $query);
$totalbooks = 0;
while($row=mysqli_fetch_array($result)){
$totalbooks = $row['totalbooks'];
}
$query ="select count(id) as issuedbooks from issue";
$result=mysqli_query($con, $query);
$issuedbooks=0;
while($row=mysqli_fetch_array($result)){
$issuedbooks= $row['issuedbooks'];
}
$sub_result = $totalbooks - $issuedbooks;
echo $sub_result;
?>
答案 2 :(得分:0)
你尝试过这样的事吗?在数据库上查询全部内容比在客户端上进行2次查询和计算要快得多
<?php
$query ="select (totalbooks.val - issuedbooks.val) as result from (select sum(copies) as val from booksdetail) totalbooks, (select count(id) as val from issue) issuedbooks";
$result=mysqli_query($con, $query);
while($row=mysqli_fetch_array($result)){
echo $row['result'];
}
?>