未找到列:即使列在laravel中定义,也会出现1054错误

时间:2016-11-04 06:17:08

标签: php mysql laravel

  

SQLSTATE [42S22]:找不到列:1054未知列   ' vpc_order_info'在' where子句' (SQL:select `sequa_transaction_id` as `vpc_transaction_no`, `merchant_order_id` as `vpc_order_info`, `date_created` as `yesterday`, `card_number` as `vpc_Card`, `amount` as `vpc_Amount`, `authorization_code` as `vpc_authorizeId` from `ipg_sentry_response` where `merchant_id` = 12136901 and `vpc_order_info` like %-% and `response_code` = 1 and `date_created` between 2016-03-22 and 2016-09-31)

我收到此错误消息,但我已按下面的定义定义了该列。

$col = array("sequa_transaction_id as vpc_transaction_no","merchant_order_id as vpc_order_info","date_created as yesterday","card_number as vpc_Card", "amount as vpc_Amount","authorization_code as vpc_authorizeId");

     $records = DB::table('ipg_sentry_response')->select($col)
     ->where('merchant_id', '=', $vpc_Merchant)
             ->where('vpc_order_info' ,'like', "%-%")            
     ->where('response_code', '=', '1')
     ->whereBetween('date_created', array($date,$date2))
     //->whereBetween('date_created', array($date,$date2))
     ->get();

有人可以帮我吗?我使用的是Laravel 5.2版本。

2 个答案:

答案 0 :(得分:1)

列别名已定义,但尚未在where caluse中使用。您可以在aliasgroup中使用order ...您需要使用列名而不是别名。试试 -

 $col = array("sequa_transaction_id as vpc_transaction_no",
        "merchant_order_id as vpc_order_info","date_created as yesterday",
        "card_number as vpc_Card", "amount as vpc_Amount",
        "authorization_code as vpc_authorizeId");

 $records = DB::table('ipg_sentry_response')->select($col)
 ->where('merchant_id', '=', $vpc_Merchant)
 ->where('merchant_order_id' ,'like', "%-%")            
 ->where('response_code', '=', '1')
 ->whereBetween('date_created', array($date,$date2))
 ->get();
  

您只能在 GROUP BY ORDER BY HAVING 子句中使用列别名。标准SQL不允许您引用 WHERE 子句中的列别名。

答案 1 :(得分:0)

正如我上面的答案所述,WHERE子句不接受别名。此外,您还可以使用GROUP BY - HAVING方法(https://laravel.com/docs/5.3/queries#ordering-grouping-limit-and-offset