DB :: raw查询问题SQLSTATE [42000]:语法错误或访问冲突:1064

时间:2017-12-26 10:26:45

标签: mysql laravel laravel-5 laravel-4 laravel-5.3

DB::raw("SELECT  'finacial_calender.id' FROM 'finacial_calender' where 'finacial_calender.F_YEAR' = 'YEAR(invoice.INV_DT)' and 'finacial_calender.F_MONTH' = 'MONTH(invoice.INV_DT)'  ")

此行有问题,但我的完整查询是

$invoice_FlagS = DB::table('invoice')
            ->join('customer', 'invoice.CUST_ID', '=', 'customer.CUST_ID')
        ->join('cmp_dsr_products', 'invoice.DSR_PROD_ID', '=', 'cmp_dsr_products.DSR_PROD_ID')
        ->join('distributor_brick', 'customer.DSR_BRICK_ID', '=', 'distributor_brick.DSR_BRICK_ID')
        ->join('head_office_brick', 'distributor_brick.HO_BRICK_ID', '=', 'head_office_brick.HO_BRICK_ID')
        ->join('territory_division', 'head_office_brick.HO_BRICK_ID', '=', 'territory_division.HO_BRICK_ID')
        ->join('master_territory', 'territory_division.TERRITORY_ID', '=', 'master_territory.TERRITORY_ID')
        ->join('districts', 'master_territory.DISTRICT_ID', '=', 'districts.DISTRICT_ID')
        ->where('invoice.COMPANY_ID', '=', $company_id)
        ->whereBetween('INV_DT', [$fromDate, $toDate])
        ->select('master_territory.TERRITORY_ID','master_territory.DIV_ID','master_territory.DISTRICT_ID','districts.REGION_ID','cmp_dsr_products.PROD_ID','invoice.INV_QTY','invoice.INV_DISCOUNT_VAL','invoice.INV_NET_VAL','invoice.INV_DT','invoice.COMPANY_ID',DB::raw("SUM( ( CASE WHEN invoice.FLAG ='S' THEN invoice.INV_NET_VAL END ) ) AS SaleAmount"),DB::raw("SUM(abs( ( CASE WHEN invoice.FLAG ='R' THEN invoice.INV_NET_VAL END )) ) AS ReturnAmount"),DB::raw('MONTH(invoice.INV_DT)'),DB::raw("SELECT  'finacial_calender.id' FROM 'finacial_calender' where 'finacial_calender.F_YEAR' = 'YEAR(invoice.INV_DT)' and 'finacial_calender.F_MONTH' = 'MONTH(invoice.INV_DT)'  "))
//      ->select('distributor_product.DSR_PRODUCT_NAME','INV_ID','INV_QTY','INV_DISCOUNT_VAL',DB::raw('sum(INV_NET_VAL)AS SaleValue'))
        ->orderBy('invoice.INV_DT', 'asc')
        ->groupBy('invoice.INV_DT','invoice.DSR_PROD_ID')
        ->get();

0 个答案:

没有答案