[英]Subtract a SUM value from a regular number to get a total in using PHP
我正在我的應用程序中設置一個“目標”顯示字段。 此示例的目標是 12000 公斤(我們處理電子垃圾)。 我使用 MySQL SUM 函數來統計我的員工輸入的所有權重,現在我想取 12000KG 目標,並將其從 SUM 值中減去以獲得差異,以便員工可以看到他們離實際 12000KG 目標有多遠. 我該怎么做?
<h3>Target: 12000KG</h3>
<h3>Current Value: <?php
$dbConnection = new PDO('mysql:dbname=ecemsmaster;host=127.0.0.1;charset=utf8', 'ecemsmaster', 'ecemsmaster');
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//The COUNT SQL statement that we will use.
$sql = "SELECT MONTH(date), SUM(subgrade + castaluminium + copper + stainlesssteel + plastic + brass + batteries + cables + lowgradePCB + mediumgradePCB + highgradePCB) AS num FROM daily_recyclables where MONTH(date)=MONTH(curdate())";
//Prepare the COUNT SQL statement.
$stmt = $dbConnection->prepare($sql);
//Execute the COUNT statement.
$stmt->execute();
//Fetch the row that MySQL returned.
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//The $row array will contain "num". Print it out.
echo $row['num'];
?> KG</h3>
<h3>Over/Under Target: **-1000KG**</h3>
不確定這是否是正確的方法,但它對我有用。
我剛剛通過 echo $row['num']; 添加了“-12000”;
<?php
$dbConnection = new PDO('mysql:dbname=ecemsmaster;host=127.0.0.1;charset=utf8', 'ecemsmaster', 'ecemsmaster');
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//The COUNT SQL statement that we will use.
$sql = "SELECT MONTH(date), SUM(subgrade + castaluminium + copper + stainlesssteel + plastic + brass + batteries + cables + lowgradePCB + mediumgradePCB + highgradePCB) AS num FROM daily_recyclables where MONTH(date)=MONTH(curdate())";
//Prepare the COUNT SQL statement.
$stmt = $dbConnection->prepare($sql);
//Execute the COUNT statement.
$stmt->execute();
//Fetch the row that MySQL returned.
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//The $row array will contain "num". Print it out.
echo $row['num'];
?> KG</h3>
<h3>Over/Under Target: <?php
$dbConnection = new PDO('mysql:dbname=ecemsmaster;host=127.0.0.1;charset=utf8', 'ecemsmaster', 'ecemsmaster');
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//The COUNT SQL statement that we will use.
$sql = "SELECT MONTH(date), SUM(subgrade + castaluminium + copper + stainlesssteel + plastic + brass + batteries + cables + lowgradePCB + mediumgradePCB + highgradePCB) AS num FROM daily_recyclables where MONTH(date)=MONTH(curdate())";
//Prepare the COUNT SQL statement.
$stmt = $dbConnection->prepare($sql);
//Execute the COUNT statement.
$stmt->execute();
//Fetch the row that MySQL returned.
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//The $row array will contain "num". Print it out.
echo $row['num']-12000;
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.