php - PHP - 根据项目名称的 SUM 编号
问题描述
我是 PHP 新手,我的脚本需要帮助,因为我没有得到首选答案。我有一个“账单”表,其中存储了销售项目的信息。如下
id | gid | drinks | no_drinks | servicetime | date
1 2 Orange 4 1 2018-08-16
2 2 Orange 2 1 2018-08-16
3 2 Orange 3 1 2018-08-16
下面的文件(BillsController.php)查询gid、servicetime和date,查询等于true。
票据控制器.php
$bi = Barz::whereRaw('gid=? and servicetime=? and date=?', array($gid, $t,date('Y-m-d')))->get();
return View::make('bills.show', compact('bi'));
下面是显示来自数据库的信息的“show.php”文件。我只会向您展示其中的一些内容。
<?php
$foods = array();
foreach($bi as $row){
$foods[] = $row->drinks;
$idadi[] = $row->no_drinks;
$unique = array_keys(array_count_values($foods));
$l = count($unique);
}
$newarr = array();
foreach ($unique as $key => $value) {
array_push($newarr, $value);
}
?>
<table class="table table-bordered" id="gt">
<tr style="background-color: #f5f5f5">
<th>Drink</th>
<th>Qty...sx</th>
<th>Time</th>
<th>@cost</th>
<th>Total@</th>
<th>
<select class="form-control active" id="tserv">
<option value="{{$row->servicetime}}">{{Bill::tm($row->servicetime)}}</option>
<select>
</th>
</tr>
<?php $total = 0; ?>
@for($i=0; $i < $l; $i++)
<tr>
<td>{{$newarr[$i]}}</td>
<td>{{$idadi[$i]}}</td> // Only problem is here
<td>{{Bill::appears($newarr[$i], $foods)}}</td>
<td>{{Bar::where('name', $newarr[$i])->first()->cost}} /=</td>
<td>{{($idadi[$i])*(Bar::where('name', $unique[$i])->first()->cost)}}/= </td>
</tr>
<?php $total = $total + (($idadi[$i])*(Bar::where('name', $newarr[$i])->first()->cost)); ?>
@endfor
<tr style="background-color: #f5f5f5">
<td ></td>
<td ></td>
<td></td>
<td><b>Total</b></td>
<td id="ttl">
{{$total}} /=
</td>
</tr>
当我运行上面的查询时,我从 ("{{$idadi[$i]}}") 得到 4,这意味着它只占用第一行,其余的没有被选中。我希望它像这样(4 + 2 + 3)= 9。
解决方案
将列添加在一起;
SELECT col1, col2, col3, (col1+col2+col3) AS Total FROM
要将行添加在一起,请使用SUM()
汇总如下;
SELECT userid, SUM(col1) AS col1_total, SUM(col2) AS col2_total FROM table GROUP BY userid
推荐阅读
- powershell - 具有循环的 powershell 任务中的 Azure DevOps task.setVariable 仅设置最后一个变量值
- javascript - 地图 JavaScript 中数字的平均值
- sparql - SPAQRL 创建排除查询
- azure-keyvault - Azure KeyVault 加密 API 终结点不返回 Base64 编码字符串
- c# - 错误:转储到表进程:找不到路径的一部分
- wpf - 如何使 mainWindow 的某些元素在其他视图中通用?
- c++ - 你将如何编写一个函数来创建一个链表?
- mysql - Mysql - 在数组中找到 JSON_CONTAINS
- azure - 如何通过具有最大数据处理限制的 API 处理大量数据
- java - Spring boot webflux R2DBC 性能问题