如何在RAW查询中访问返回值?

时间:2019-05-07 11:36:01

标签: mysql laravel-5 eloquent laravel-query-builder

我在Laravel querybuilder中有一个RAW查询,我想使用从 RAW SELECT 语句

返回的值添加一个计算列。

请参见示例:

let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr || [])];

console.log(arr2);

在上述 SELECT 语句中,我要使用total_qty_sold like

**
DB::raw("(SELECT SUM(IF(transactions.type='sell', TSL.quantity - TSL.quantity_returned , -1* TPL.quantity) ) 
FROM transactions JOIN transaction_sell_lines AS TSL ON transactions.id=TSL.transaction_id LEFT JOIN 
purchase_lines AS TPL ON transactions.id=TPL.transaction_id
WHERE transactions.status='final' AND transactions.type='sell' $date_pick AND transactions.location_id='$location_id'
AND (TSL.variation_id=v.id OR TPL.variation_id=v.id)) as total_qty_sold"),

 'u.short_name as unit',

DB::raw('SUM((transaction_sell_lines.quantity -
transaction_sell_lines.quantity_returned) * 
transaction_sell_lines.unit_price_inc_tax) as subtotal'))
  ->groupBy('p.id')
  ->make(true);
**

MYSQL抛出错误未找到列'total_qty_sold'等...

因为原始查询需要一些条件语句,并且我在同一个查询中有多个,所以无法使用join获得要求的值

我该怎么做才能从原始查询中获取列。

NB: 这是从一个大查询中提取的。

0 个答案:

没有答案