好啦 问题自己想办法解决了 解决方法是用LEFT JOIN跟GROUP BY
下面是sql
SELECT
d.id,
d.parent_id,
d.name,
COUNT( a.employee ) AS nums
FROM
toadmin_auth_department d
LEFT JOIN touser_auth_user u ON d.id = u.department_id
LEFT JOIN business a ON a.employee = u.id and a.record_time >= '2020-11-01 09:51:24'
GROUP BY
d.id,d.parent_id,d.name;
TP6是这样写
$field = 'd.id, d.parent_id, d.name, COUNT( a.employee ) AS nums';
$data['list'] = Db::name("toadmin_auth_department")
->alias('d')
->leftJoin(['touser_auth_user' => 'u'], 'd.id = u.department_id')
->leftJoin(['business' => 'a'], "a.employee = u.id and a.record_time >= '".$record_time."'")
->group('d.id,d.parent_id,d.name')
->where($where)->field($field)->page($params['page'], $params['size'])->order('d.id desc')->select()->toArray();
得出的数据
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…