laravel - Laravel Controller 中查询内的条件计数
问题描述
我正在使用下面的查询从几个表中获取教师信息。在selectteacehrs表中有单元格user_id
和head_id
,用户/教师可能在其他行中有一些值,这些值可以根据 找到head_id
。
例如:
$distributed = SelectedTeacehr::where('head_id', $teacher->id)->sum('no_of_sheet');
我只需要在下面的查询中使用这个计数。
$teacehrs = DB::select("
SELECT
users.name as ex_name,
users.id as id,
districts.name as ex_district,
thanas.name as ex_thana,
users.account_br_rout as ex_bank_routing_no,
selectteacehrs.no_of_sheet as sheet_count,
selectteacehrs.head_id as head,
subjects.cost_per_sheet as per_sheet_cost,
LTRIM(RTRIM(subjects.name)) as ex_sub,
((thanas.ta_cost+thanas.da_cost)+(selectteacehrs.no_of_sheet*subjects.cost_per_sheet)) as total_cost
FROM selectteacehrs
INNER JOIN users ON users.id = selectteacehrs.user_id
INNER JOIN thanas ON thanas.code = users.thana_code
INNER JOIN districts ON districts.code = users.district_code
INNER JOIN subjects ON selectteacehrs.sub_code = subjects.code
{$where}
LIMIT {$from},{$perpage}
");
解决方案
推荐阅读
- haproxy - HAProxy 上子域和通配符域的特定路由
- windows - IllegalOperation:试图在 Windows 中的只读目录 MongoDB 上创建锁定文件
- xml - 如何连接 with-param 输出和字符串?
- java - springdoc-openapi 应用默认全局 SecurityScheme 可能吗?
- php - 在 Laravel 中获取没有特定标签的帖子?
- python - 无法更新 conda 环境
- c# - 有没有办法在c#中注释掉字符串的一部分?
- python - 正则表达式单词前的第一个字符
- docker - 使用 Docker for Windows 备份、恢复或迁移数据卷
- c - 在 C 中读取 python 生成的二进制数组时出现分段错误(核心转储)错误