首页 > 解决方案 > Laravel Controller 中查询内的条件计数

问题描述

我正在使用下面的查询从几个表中获取教师信息。在selectteacehrs表中有单元格user_idhead_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}
        ");

标签: laraveleloquent

解决方案


推荐阅读