Laravel DB查询组按输出不同

时间:2017-07-11 11:33:44

标签: mysql laravel-5 group-by

我在Laravel项目中有以下实现的查询,如果我在控制台中复制粘贴,但查询正在运行,但是DB对象在子查询group by zeit_von上设置为group by zeit_von is null

$data = DB::table('e_supply AS s')
            ->select(
                    DB::raw('count(s.employee) as anzahl_employee'),
                    DB::raw('group_concat(a.account_lastname order by a.account_lastname ) AS employee_names'),
                    DB::raw('date_format(s.zeit_von, "%Y") as y'),
                    DB::raw('date_format(s.zeit_von, "%m") as m'),
                    DB::raw('date_format(s.zeit_von, "%d") as d'),
                    DB::raw('date_format(s.zeit_von, "%h") as h')
            )
            ->leftJoin('phpgw_accounts AS a', 'a.account_id', '=', 's.employee')
            ->where('deleted', 0)
            ->whereIn('supply_status', [1,3])
            ->where(
                DB::raw('( select count(cal_id) from phpgw_cal_utf8  as c
                        where c.owner=s.employee
                        and deleted=0
                        and date_format(s.zeit_von, "%d.%m.%y")=date_format(from_unixtime(c.mdatetime), "%d.%m.%y")
                        and c.category="Krank"
                    )<1 and date_format((s.zeit_von), "%Y")="'.$year.'" group by zeit_von'
               )
            )
            ->get();

在这种情况下我缺少什么?

1 个答案:

答案 0 :(得分:0)

使用whereRaw进行子查询可以解决我遇到的问题

$data = DB::table('e_supply AS s')
            ->select(
                'zeit_von as raw_time',
                DB::raw('count(distinct s.employee) as anzahl_employee'),
                DB::raw('group_concat(a.account_lastname order by a.account_lastname ) AS employee_names'),
                DB::raw('date_format(s.zeit_von, "%Y") as y'), DB::raw('date_format(s.zeit_von, "%m") as m'),
                DB::raw('date_format(s.zeit_von, "%d") as d'), DB::raw('date_format(s.zeit_von, "%h") as h')
            )
            ->leftJoin('phpgw_accounts AS a', 'a.account_id', '=', 's.employee')
            ->where('deleted', 0)
            ->whereIn('supply_status', [1, 3])
            ->whereRaw(
                DB::raw('( select count(cal_id) from phpgw_cal_utf8  as c
                        where c.owner=s.employee
                        and deleted=0
                        and date_format(s.zeit_von, "%d.%m.%y")=date_format(from_unixtime(c.mdatetime), "%d.%m.%y")
                        and c.category="Krank"
                    )<1'
                )
            )
            ->whereYear('s.zeit_von', $year)
            ->groupBy('zeit_von')
            ->get();