Requirements: need to check account table each account in report_date association table in which the month field order_num and bi_ci_fee and the record of (a list query can be successful because the problem did not find a little bit small, but unfortunately demolished Open writing, for viewing only)
account table
id
admin_id
channel
account
name
addtime
1
1001
PAIC
zhangsan
Zhang San
2021-03-01 00:00:00
2
1002
CPIC
lysis
Li Si
2021-03-01 00:00:00
3
1003
PICC
wangwu
Wang Wu
2021-03-01 00:00:00
system_user表
id
nick
1001
zhangsss
1002
lisii
1003
wangww
report_date表
id
account_id
order_date
bi_ci_fee
order_num
addtime
1
1
2021-03-01
2222.00
15
2021-03-01 00:00:00
2
2
2021-03-01
2500.00
18
2021-03-01 00:00:00
3
3
2021-03-01
3888.00
12
2021-03-01 00:00:00
> 先查询出所有的 account 账户 再遍历根据 id/account_id 在 report_date 查询 该月记录
> account 的 admin_id 关联 system_user 的 id 联表查询只为得到 nick
$accounts= Db::table("account ")->alias("a")->join("system_user u","u.id=a.admin_id")->field("a.name,a.id as account_id,u.nick as team_name")->group("name")->where("a.admin_id >0")->select();> 遍历account账户 根据每条account 的 name 来查询 并且 groupby name
> 因为有 同一个姓名但是有多个 account 账户的.... 所以 根据 name 求 sum
$last_month=date("Y-m",strtotime("-1 month"));//求上个月的数据foreach($accountsas$key=>&$account){
$account['order_num']= Db::table("tm_order_report_date")->alias("ord")->Join("tm_account a ","ord.account_id=a.id")->where(['a.name'=>$account['name']])->where('order_date','like','%'.$last_month.'%')->sum("order_num");$account['total_bi_ci_fee']=number_format(Db::table("tm_order_report_date")->alias("ord")->Join("tm_account a ","ord.account_id=a.id anda.name='{
$account['name']}'")->where('order_date','like','%'.$last_month.'%')->sum("bi_ci_fee"),2);}> 这样就取到了需要 数据 但是 没有排序 我们需要按照 total_bi_ci_fee 来排序
> 排序方法 底部原文链接 https://blog.csdn.net/lz0426001/article/details/40784109/**
* 对查询结果集进行排序
* @access public
* @param array $list 查询结果
* @param string $field 排序的字段名
* @param array $sortby 排序类型
* asc正向排序 desc逆向排序 nat自然排序
* @return array
*/functionlist_sort_by($list,$field,$sortby='asc'){
if(is_array($list)){
$refer=$resultSet=array();foreach($listas$i=>$data)$refer[$i]=&$data[$field];switch($sortby){
case'asc':// 正向排序asort($refer);break;case'desc':// 逆向排序arsort($refer);break;case'nat':// 自然排序natcasesort($refer);break;}foreach($referas$key=>$val)$resultSet[]=&$list[$key];return$resultSet;}returnfalse;}> 调用下排序方法
$sortedAccounts=$this->list_sort_by($accounts,"total_bi_ci_fee",'desc');
此方法用于 获取前面24条 将查询出来的数据拆分前面一部分
$sortedAccounts=array_slice($sortedAccounts,0,24);
to sum up
$last_month=date("Y-m",strtotime("-1 month"));//求上个月的数据
Db::table("tm_account")->alias("a")->leftJoin("tm_order_report_date d","a.id=d.account_id")->leftJoin("system_user su","a.admin_id=su.id")->field("a.name,su.email as team_name,sum(if(DATE_FORMAT(order_date,'%Y-%m')='{
$last_month}',d.order_num,0)) as order_num,sum(if(DATE_FORMAT(order_date,'%Y-%m')='{
$last_month}',d.bi_ci_fee,0)) as total_bi_ci_fee")->group("a.name")->order("total_bi_ci_fee desc")->where("a.admin_id >0 and a.name is not null")->limit(0,24)->select();
一条SQL语句就完事了 遍历毛线... 写联表查询要注意细节....