如何在 mysql 和 PHP 中的 table 的列中添加每个数据?
How to add each data inside a column of a table in both mysql and PHP?
我有一个列名称 'sale',在销售列中有每个客户的销售列表。我如何添加每个客户的所有销售额,然后在 table 下回应总销售额?
> ?php
include('conn.php');
$sql = "SELECT * FROM sales";
$result = mysqli_query($conn,$sql);
$count = mysqli_num_rows($result);
if($count>0)
{
echo "<html><head></head><body><table border=1>
<th>CODE</th>
<th>Employee Name</th>
<th>Customer</th>
<th>Sales</th>";
while($row = mysqli_fetch_assoc($result))
{
echo"<tr>";
echo"<td>".$row['empcode']."</td>
<td>".$row['fullname']."</td>
<td>".$row['customercode']."</td>
<td>".$row['sales']."</td></tr>";
}
echo"</table>";
}
?>
您需要在循环外添加一个变量并在循环的每次迭代中递增它
$totalSales = 0;
if($count>0)
{
echo "<html><head></head><body><table border=1>
<th>CODE</th>
<th>Employee Name</th>
<th>Customer</th>
<th>Sales</th>";
while($row = mysqli_fetch_assoc($result))
{
echo"<tr>";
echo"<td>".$row['empcode']."</td>
<td>".$row['fullname']."</td>
<td>".$row['customercode']."</td>
<td>".$row['sales']."</td></tr>";
$totalSales += $row['sales'];
}
echo"</table>";
}
对于 MySQL 你可以使用 SUM 分组函数。
例如SELECT SUM(sales) as total FROM table_name;
而对于 PHP,您可以这样做:
$total = 0;
while($row = mysqli_fetch_assoc($result))
{
$total += $row['sales'];
echo"<tr>";
echo"<td>".$row['empcode']."</td>
<td>".$row['fullname']."</td>
<td>".$row['customercode']."</td>
<td>".$row['sales']."</td></tr>";
}
// Print total at the end
echo "<tr>";
echo "<td colspan='3'>Total</td>";
echo "<td>".$total."</td>";
echo "</tr>";
希望对您有所帮助。
运行
$sql = "SELECT *, SUM(sales) AS total_sale FROM
销售GROUP BY
客户代码";
而不是
$sql = "SELECT * FROM sales";
我有一个列名称 'sale',在销售列中有每个客户的销售列表。我如何添加每个客户的所有销售额,然后在 table 下回应总销售额?
> ?php
include('conn.php');
$sql = "SELECT * FROM sales";
$result = mysqli_query($conn,$sql);
$count = mysqli_num_rows($result);
if($count>0)
{
echo "<html><head></head><body><table border=1>
<th>CODE</th>
<th>Employee Name</th>
<th>Customer</th>
<th>Sales</th>";
while($row = mysqli_fetch_assoc($result))
{
echo"<tr>";
echo"<td>".$row['empcode']."</td>
<td>".$row['fullname']."</td>
<td>".$row['customercode']."</td>
<td>".$row['sales']."</td></tr>";
}
echo"</table>";
}
?>
您需要在循环外添加一个变量并在循环的每次迭代中递增它
$totalSales = 0;
if($count>0)
{
echo "<html><head></head><body><table border=1>
<th>CODE</th>
<th>Employee Name</th>
<th>Customer</th>
<th>Sales</th>";
while($row = mysqli_fetch_assoc($result))
{
echo"<tr>";
echo"<td>".$row['empcode']."</td>
<td>".$row['fullname']."</td>
<td>".$row['customercode']."</td>
<td>".$row['sales']."</td></tr>";
$totalSales += $row['sales'];
}
echo"</table>";
}
对于 MySQL 你可以使用 SUM 分组函数。
例如SELECT SUM(sales) as total FROM table_name;
而对于 PHP,您可以这样做:
$total = 0;
while($row = mysqli_fetch_assoc($result))
{
$total += $row['sales'];
echo"<tr>";
echo"<td>".$row['empcode']."</td>
<td>".$row['fullname']."</td>
<td>".$row['customercode']."</td>
<td>".$row['sales']."</td></tr>";
}
// Print total at the end
echo "<tr>";
echo "<td colspan='3'>Total</td>";
echo "<td>".$total."</td>";
echo "</tr>";
希望对您有所帮助。
运行
$sql = "SELECT *, SUM(sales) AS total_sale FROM
销售GROUP BY
客户代码";
而不是
$sql = "SELECT * FROM sales";