GROUP_CONCAT的Laravel代码不起作用

时间:2019-03-11 04:14:17

标签: mysql laravel

我想使用MySQL在Laravel项目中创建一个逗号分隔的结果。我知道我必须使用GROUP_CONCAT,并且使用工作台使它在mysql上可以完美地工作。但是在Laravel代码上使用它时出现错误。 请在下面查看我的实际代码。当我删除GROUP_CONCAT行/部分时,我的代码运行正常。

调用API时的错误代码是:500(内部服务器错误)

 $result = Rmim\WhQcHdr::from('wh_qc_hdrs as hdr')->select(
        'rr.TrxDate as RR Date',
        'hdr.rr_no as RR No',
        'hdr.inspected_at as Date of Inspection',
        'hdr.qcir_no as QCIR No',
        'hdr.item_code as Item Code',
        'item.ItemDesc as Item Description',
        'hdr.supplier_code as Supplier Code',
        'con.ContactName as Supplier Name',
        'hdr.reject_reason as Reason of Rejection',
        'hdr.cancel_reason as Reason of Cancellation',
        \DB::raw('(CASE WHEN hdr.qc_status="A" THEN "Passed" 
                    WHEN hdr.qc_status="CA" THEN "Conditionally Approved"
                    WHEN hdr.qc_status="R" THEN "Rejected"
                    WHEN hdr.qc_status="C" THEN "Cancelled"
                    ELSE "N/A"
                END) as Disposition'),

        -- line below causes an error when I call my api, it works perfectly if I remove it.
        \DB::raw('GROUP_CONCAT(dtl.parameters) as Failed Parameters')
    )

    ->leftJoin('rminv.tbl_rmtrninv as rr', function ($join){
        $join->on('hdr.rr_no', '=','rr.trxnum')
        ->where('rr.TransID', '=', 2);
    })
    ->leftJoin('rminv.tbl_rmmstitem as item', 'hdr.item_code', '=', 'item.ItemCode')
    ->leftJoin('fginv.tbl_fgmstcontacts as con','hdr.supplier_code','=','con.ContactCode')
    ->leftJoin('crp.wh_qc_dtls as dtl', function ($join){
        $join->on('hdr.id', '=','dtl.qc_id')
        ->where('dtl.result', '=', 0);
    })
    ->groupBy('rr.TrxDate',
        'hdr.rr_no',
        'hdr.inspected_at',
        'hdr.qcir_no',
        'hdr.item_code',
        'item.ItemDesc',
        'hdr.supplier_code',
        'con.ContactName',
        'hdr.reject_reason',
        'hdr.cancel_reason',
        \DB::raw('(CASE WHEN hdr.qc_status="A" THEN "Passed" 
            WHEN hdr.qc_status="CA" THEN "Conditionally Approved"
            WHEN hdr.qc_status="R" THEN "Rejected"
            WHEN hdr.qc_status="C" THEN "Cancelled"
            ELSE "N/A"
        END)'))
    ->orderBy('hdr.created_at', 'Desc');

先谢谢您

1 个答案:

答案 0 :(得分:0)

我刚刚将别名从Failed Parameters更改为Failed_Parameters变成了1个字,错误消失了。我真的不知道为什么,因为在其他字段中,超过1个单词的别名很好,但是在GROUP_CONCAT部分却出现错误。

如果有人能够解释这一点,请这样做,这将是一个很大的帮助。