【问题标题】:Whats wrong with my code Laravel Callback function GroupBy Month我的代码 Laravel 回调函数 GroupBy Month 有什么问题
【发布时间】:2019-06-21 03:34:12
【问题描述】:

我要数我的pallet_condition 0,1,2,3 并按月分组 但我总是遇到这样的错误,请帮助我的代码有什么问题。 我的 date_format 有问题吗?

我的错误

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in group statement is ambiguous (SQL: select count(*) as aggregate from `liip_psrm` inner join `liip_psrm_items` on `liip_psrm`.`id` = `liip_psrm_items`.`psrm_items_id` where date(`liip_psrm_items`.`created_at`) >= 2018-10-09 and date(`liip_psrm_items`.`created_at`) <= 2019-01-31 and `liip_psrm`.`status` = 0 and `liip_psrm`.`customer_id` = 14 and `pallet_condition` = 0 group by WEEK(created_at) order by WEEK(created_at) asc) 

我的回调函数

$problem_condition_computations = function($condition) use ($psrm_maintenance, $start, $end, $customers){
        return DB::table('liip_psrm')
        ->join('liip_psrm_items', 'liip_psrm.id', '=', 'liip_psrm_items.psrm_items_id')
        ->whereDate('liip_psrm_items.created_at', '>=', $start)
        ->whereDate('liip_psrm_items.created_at', '<=', $end)
        ->select(
                DB::raw("DATE_FORMAT(liip_psrm_items.created_at, '%Y-%m-%d') AS dates
                "))
                ->where('liip_psrm.status','=', 0)
                ->where('liip_psrm.customer_id','=',$customers)
                ->where('pallet_condition', $condition)
                ->groupBy('dates')   
                ->count();
      };

【问题讨论】:

    标签: mysql eloquent laravel-4.2 function-query


    【解决方案1】:

    您在 groupBy 中使用的“日期”字段是别名,因此它无法识别它并且无法按预期工作。

    和你在选择中使用的一样

    DB::raw("DATE_FORMAT(liip_psrm_items.created_at, '%Y-%m-%d') AS dates")
    

    把它放在 GroupBy & 它会起作用的。

    希望,这会有所帮助,如果您仍然遇到困难,请告诉我。

    【讨论】:

    • 不客气!那么请检查它是否正确!
    猜你喜欢
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2015-04-29
    • 2011-07-28
    相关资源
    最近更新 更多