mysql - Laravel DB 查询组按输出不同

标签 mysql laravel-5 group-by

我在 Laravel 项目中实现了以下查询,如果我在控制台中复制粘贴,则该查询正在工作,但数据库对象在子查询 group by zeit_von 上设置为 group by zeit_von为空

$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();

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

最佳答案

使用 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();

关于mysql - Laravel DB 查询组按输出不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45033402/

相关文章:

php mysql 位字段

mysql - mysql 有路径分隔符功能吗?

mysql - SQL分组相同的名字

python - 将 A 列列表中的最后一个值添加到 b 列列表中

mysql - 如何在mysql中使用2列进行分组

php - 在 sql 查询中定义 ORDER

php - 拉维尔 5 : redirect to an external link outside of localhost/server

php - Laravel Eloquent - 查询构建器找不到具有功能的列

php - 如何为 Laravel 5 建立多层 hasMany() 关系?

mysql - mysql中一张表的重复数据的平均值