• 一、需求
    • 1、要求
    • 2、结果
    • 3、已知
  • 二、步骤
  • 三、代码
  • 四、打印
    • 1、打印 $result
    • 2、浏览器打印截图
    • 3、Excel下载截图

一、需求

1、要求

  • 根据来源、归属,统计订单、售后单的金额

2、结果

  • 大概的结果如下图所示

3、已知

  • 订单表名 order,字段 id,source_id,belong_id,price,…
  • 售后单表名 after_sale,字段 id,source_id,belong_id,price,…

二、步骤

  • 订单和售后是2张表,我们需要分别查出2张表的数据再处理这2张表的数据
  • 1、分组source_id,belong_id,求和price;查询出订单,售后单表的数据
  • 2、把数据处理成 $data[$sourceId][$belongId] = $price 的格式
  • 3、获取(合并去重) $datakey,$sourceIds
  • 4、循环 $sourceIds,判断订单,售后单 sourceId 是否存在
  • 5、根据sourceId 获取$orderSource,$afterSaleSource数据,合并去重key获取到$belongIds
  • 6、循环$belongIds获取到最后的数据,$result接收
  • 7、调打印方法,打印$result结果数据

三、代码

 public function test(){# 订单数据,group订单表source_id,belong_id字段,求和price# select source_id,belong_id,sum(price) as price from order group by source_id,belong_id$orderList = [['source_id' => 1, 'belong_id' => 1, 'price' => 10],['source_id' => 1, 'belong_id' => 2, 'price' => 20],['source_id' => 2, 'belong_id' => 1, 'price' => 30],['source_id' => 2, 'belong_id' => 3, 'price' => 30],['source_id' => 3, 'belong_id' => 1, 'price' => 40],];# 把订单表的数据处理成$orderData[$sourceId][$belongId] = $price;$orderData = [];foreach ($orderList as $value){$orderData[$value['source_id']][$value['belong_id']] = $value['price'];}# 售后表数据,group售后表source_id,belong_id字段,求和price# select source_id,belong_id,sum(price) as price from after_sale group by source_id,belong_id$afterSaleList = [['source_id' => 2, 'belong_id' => 1, 'price' => 11],['source_id' => 3, 'belong_id' => 2, 'price' => 21],['source_id' => 4, 'belong_id' => 3, 'price' => 31],['source_id' => 5, 'belong_id' => 1, 'price' => 41],];# 把售后表的数据处理成$afterSaleData[$sourceId][$belongId] = $price;$afterSaleData = [];foreach ($afterSaleList as $value){$afterSaleData[$value['source_id']][$value['belong_id']] = $value['price'];}# 获取订单表和售后表的$sourceId并集$sourceIds$sourceIds = array_unique(array_merge(array_keys($orderData), array_keys($afterSaleData)));# 循环来源ID$result = [];foreach ($sourceIds as $sourceId){# 获取订单、售后表来源下的数据$orderSource = empty($orderData[$sourceId]) ? [] : $orderData[$sourceId];$afterSaleSource = empty($afterSaleData[$sourceId]) ? [] : $afterSaleData[$sourceId];# 获取订单、售后表来源下的归属ID并集$belongIds = array_unique(array_merge(array_keys($orderSource), array_keys($afterSaleSource)));# 循环归属IDforeach ($belongIds as $belongId){# 获取订单金额、售后金额$orderPrice = empty($orderSource[$belongId]) ? 0 : $orderSource[$belongId];$afterSalePrice = empty($afterSaleSource[$belongId]) ? 0 : $afterSaleSource[$belongId];# 最终数据拼接$result[] = ['source' => "来源 [ {$sourceId} ]",'belong' => "归属 [ {$belongId} ]",'order_price' => $orderPrice,'after_sale_price' => $afterSalePrice];}}# var_export($result);die;$this->tableDisplay($result);}/*** 表格显示* @param $result 需要展示的数据* @param bool $isDownload 是否下载Excel:true 是;false 否*/public function tableDisplay($result, $isDownload = false){if ($isDownload){$titleName = '订单售后金额统计-'.date('Y-m-d-H_i_s');header("Content-type:application/vnd.ms-excel;charset=GBK");header("Content-Disposition:filename={$titleName}.xls");}# 表格拼接$tableHtml = <<<EOF
<table border="1"><thead><tr ><th>来源</th><th>归属</th><th>订单金额</th><th>售后金额</th></tr></thead><tbody>
EOF;foreach ($result as $value){$tableHtml .= <<<EOF
<tr><td>{$value['source']}</td><td>{$value['belong']}</td><td>{$value['order_price']}</td><td>{$value['after_sale_price']}</td>
</tr>
EOF;}$tableHtml .='</tbody></table>';echo $tableHtml;    //输出表格内容exit();}public function example(){$tableHtml = <<<EOF
<table border="1"><thead><tr ><th>来源</th><th>归属</th><th>订单金额</th><th>售后金额</th></tr></thead><tbody><tr><td>来源X</td><td>归属X</td><td>XX</td><td>XX</td></tr></tbody>
</table>
EOF;echo $tableHtml;}

四、打印

1、打印 $result

array (0 => array ('source' => '来源 [ 1 ]','belong' => '归属 [ 1 ]','order_price' => 10,'after_sale_price' => 0,),1 => array ('source' => '来源 [ 1 ]','belong' => '归属 [ 2 ]','order_price' => 20,'after_sale_price' => 0,),2 => array ('source' => '来源 [ 2 ]','belong' => '归属 [ 1 ]','order_price' => 30,'after_sale_price' => 11,),3 => array ('source' => '来源 [ 2 ]','belong' => '归属 [ 3 ]','order_price' => 30,'after_sale_price' => 0,),4 => array ('source' => '来源 [ 3 ]','belong' => '归属 [ 1 ]','order_price' => 40,'after_sale_price' => 0,),5 => array ('source' => '来源 [ 3 ]','belong' => '归属 [ 2 ]','order_price' => 0,'after_sale_price' => 21,),6 => array ('source' => '来源 [ 4 ]','belong' => '归属 [ 3 ]','order_price' => 0,'after_sale_price' => 31,),7 => array ('source' => '来源 [ 5 ]','belong' => '归属 [ 1 ]','order_price' => 0,'after_sale_price' => 41,),
)

2、浏览器打印截图

3、Excel下载截图

PHP统计订单表,订单售后表金额相关推荐

  1. 【实时数仓】DWM层订单宽表之需求分析、订单和订单明细关联源码

    文章目录 一 DWM层-订单宽表 1 需求分析与思路 2 订单和订单明细关联代码实现 (1)从Kafka的dwd层接收订单和订单明细数据 a 创建订单实体类 b 创建订单明细实体类 c 在dwm包下创 ...

  2. 3.卡券、直充订单详情(post 表单提交)

    4.回调通知 参数名 类型 参数说明 status string 充值订单号 order_no string 官方订单号 cms_order_num string 对接方订单号(cms用户对接方) s ...

  3. 卡券、直充订单列表(post 表单提交)接口

    5.卡券.直充订单列表(post 表单提交) 网关URL:https://router.wikeyun.cn/rest/Quanyi/myOrder 参数名 类型 必填 参数说明 store_id s ...

  4. 新零售mysql设计 订单表 订单详情表

    作者:陈业贵 华为云享专家 51cto(专家博主 明日之星 TOP红人) 阿里云专家博主 文章目录 sql 订单表 数据 订单详情表 数据: 订单号与流水号有什么不同? 订单表(解析) id int ...

  5. 订单系统订单表设计方案

    一年前,在上一家公司接手了一个含有订单系统的项目,业务并不复杂,但是当时令我比较困惑的是订单表的设计. 困惑的点主要是随着订单量增加,单表的存储能力将达到瓶颈,必然要采用分表的方案,那么按照什么维度拆 ...

  6. SQL | 多表连接 | 销售订单+产品明细+销售网点表

    创建表 "销售订单表"记录了销售情况,每一张数据表示哪位顾客.在哪一天.哪个网点购买了什么产品,购买的数量是多少,以及对应产品的零售价 create table if not ex ...

  7. Java互联网架构-Mysql分库分表订单生成系统实战分析

    分库分表的必要性 首先我们来了解一下为什么要做分库分表.在我们的业务(web应用)中,关系型数据库本身比较容易成为系统性能瓶颈,单机存储容量.连接数.处理能力等都很有限,数据库本身的"有状态 ...

  8. mysql 多维度分表_亿级订单数据分库分表设计方案(满足多维度查询:订单号、用户、商家、渠道)...

    根据业务初步预估订单业务量,每天500万的数据.我们将订单数据划分为了2大类型:分别为热数据和冷数据. 热数据:1个月内的订单数据,查询实时性较高; 冷数据:归档订单数据,查询频率不高; 根据实际业务 ...

  9. 第二章--电商设计表订单实体-电商项目

    订单主表 订单详情表 购物车表 仓库信息表 商品仓库表 物流信息表

  10. 入库订单(组合关系、主从表)模型

    一共涉及到4张表,仓库,入库订单.入库订单明细,即时库存 1.仓库实体类 @Entity @Table(name="depot") public class Depot exten ...

最新文章

  1. css transform旋转属性
  2. “sockaddr_in”:“struct”类型重定义
  3. LEGv8指令集中分支和跳转的地址范围
  4. 【HTML+CSS练习】画一个条件查询
  5. 笔记:C++重载++前后区分
  6. 005 反转单链表(迭代递归)
  7. Cannot forward after response has been committed问题解决及分析
  8. 量子计算还没搞懂,光子计算又要来统治世界?
  9. oracle 10g 配置asm,在Oracle Linux 4.7上安装配置Oracle 10g ASM数据库
  10. SVN配置花生壳远程访问
  11. 加密软件不能安装软件
  12. 泰坦尼克 数据集_Kaggle-泰坦尼克-学习心得(高分容易,理解很难)——第1篇...
  13. 最小二乘法为什么使用误差平方和
  14. 【 Android 10 生物识别 】系列 -- Fingerprint_指纹录入流程
  15. js 输入数字金额同步转换为大写金额方法
  16. 安装d3dx9 43.dll后显示 请确保该二进制存储在指定的路径中
  17. 为什么在线客服系统很重要
  18. DBL_EPSILON和 FLT_EPSILON
  19. 人工智能时代对会计行业的改变与反思
  20. 微信端中的企业号、订阅号、服务号之前的区别

热门文章

  1. centos8网络配置开启wifi_在centos 8中安装各种路由协议
  2. 作为Scala语法糖的设计模式
  3. java读取properties文件详解
  4. python批量下载静态页面_Python selenium如何打包静态网页并下载
  5. 台达cp2000的面板怎么调节_吊灯怎么安装 吊灯怎么固定在顶上的
  6. 计算机专业认证协会,2017计算机类专业认证委员会工作总结会在京召开
  7. Android 软件重新加载,【BUG系列】Android 点击 Home 键后再点击 APP图标,APP 重新启动了...
  8. matlab曲线图导出,从Matlab的Figure中导出数据的办法
  9. python兔子编程_少儿编程分享:手把手教你用Python编写兔獾大作战(完)
  10. 关于left join 一些测试